使用通配符子域进行 Mod 重写?

发布于 2024-07-07 22:57:12 字数 141 浏览 8 评论 0原文

我打开了通配符子域,但是我只是不知道 mod_rewrite 达到编写此内容所需的程度。 谁能告诉我如何制作它,以便除 www 之外的任何内容都不会进入主站点,但除此之外的任何子域都会进入 /script/index.php?username=$username ?

I have Wild Card Subdomains on, however I just do not know mod_rewrite to the extent that is required to write this. Can anyone tell me how to make it so anything other than www and nothing go to the main site but any subdomain other than that go to /script/index.php?username=$username?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2024-07-14 22:57:12

$username 变量应该来自哪里?

假设主站点的 URL 为 http://www.example.com/main_site.php 并且您正在使用此外部目录上下文(即不在 .htaccess 中也不在指令中)。 如果它在 .htaccess 中,请删除前导 /(例如,将其设为 main_site.php)。

我认为这不会立即起作用,因为有许多不明确的变量(用户名从哪里来?,如何处理请求的其余部分,将其作为参数传递?,这是 htaccess 还是主配置?),但是希望能让你有一个想法:

#Turn on the rewrite engine
RewriteEngine On
#Check accessed domain, if it's either www.example.com or
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC,OR]
#example.com
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
#and the requested URL does not contain script you'll be accessing to avoid looping
RewriteCond %{REQUEST_URI} !main_site.php
#Then we tell that everything matching the above will go to main_site.php
RewriteRule ^ /main_site.php [L]
#If the request is not asking for main_site.php nor index.php
RewriteCond %{REQUEST_URI} !main_site.php
RewriteCond %{REQUEST_URI} !index.php
#We go to /script/index.php (username will be empty, becase we don't know 
#where to get it from)
RewriteRule ^ /script/index.php?username=$username [L]

Where does the $username variable supposed to come from??

Supposing the URL of the main site is http://www.example.com/main_site.php and that you are using this outside Directory context (ie, not in .htaccess nor in a <Directory> directive). If it is in .htaccess remove the leading / (make it just main_site.php, for example).

I reckon this will not work right away because there are many non clear variables (where does username come from?, what to do about the rest of the request, pass it as a parameter?, is this htaccess or main config?), but hopefully will get you an idea:

#Turn on the rewrite engine
RewriteEngine On
#Check accessed domain, if it's either www.example.com or
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC,OR]
#example.com
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
#and the requested URL does not contain script you'll be accessing to avoid looping
RewriteCond %{REQUEST_URI} !main_site.php
#Then we tell that everything matching the above will go to main_site.php
RewriteRule ^ /main_site.php [L]
#If the request is not asking for main_site.php nor index.php
RewriteCond %{REQUEST_URI} !main_site.php
RewriteCond %{REQUEST_URI} !index.php
#We go to /script/index.php (username will be empty, becase we don't know 
#where to get it from)
RewriteRule ^ /script/index.php?username=$username [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文