0

我需要将所有子域重定向到特定页面,而无需实际更改 URL,因为我将根据 URL 中的子域在此特定页面上显示不同的内容。

假设我的网站位于 testdomain.com/site1/

我希望将所有子域,例如 xyz.testdomain.com/site1/ 甚至 xyz.testdomain.com 重定向到http://testdomain.com/site1/index.php/test.php的特定页面

然后浏览器需要加载http://testdomain.com/site1/index.php/test.php,但 URL 仍然是 xyz.testdomain.com。

这样做的目的是让某人可以访问 abc.testdomain.com 或 xyz.testdomain.com,两者都会将用户带到 testdomain.com/site1/index.php/test.php,然后在 test.php,我有一些代码可以抓取 URL,如果 url 是 abc.testdomain.com,它将显示某些内容,而如果子域是 xyz.testdomain.com,它将显示不同的内容。

这是我可以在 htaccess 中做的事情吗?如果是这样,怎么做?

4

1 回答 1

0

使用mod_rewrite你可以一起破解它。

# Step 1: If the user went to example.com or www.example.com
# then we don't want to redirect them. (S=1 says skip the next rule)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^ - [S=1]

# Step 2: Anything else is redirected to our catcher script.

# Option 1: keeps the path they went to, but discards the domain
# i.e. xyz.example.com/abc/def.txt => /var/www/cgi-bin/abc/def.txt
RewriteRule ^/?(.*) /var/www/cgi-bin/$1 [QSA,L]

# Or Option 2: take all requests to the same file
# i.e. xyz.example.com/abc/def.txt => /var/www/cgi-bin/myfile.php
RewriteRule ^ /var/www/cgi-bin/myfile.php [QSA,L]

QSA告诉它转发查询字符串,L告诉它停止寻找更多的重定向(不是绝对必要的,但如果你有很多这样的事情发生,有时会有所帮助)。

您还可以将变量作为查询参数传递给脚本,并且QSA标志确保它们不会替换原始值;

# xyz.example.com/abc/def.txt => /var/www/cgi-bin/myfile.php?host=xyz.example.com&path=/abc/def.txt
RewriteRule ^/?(.*) /var/www/cgi-bin/myfile.php?host=%{HTTP_HOST}&path=/$1 [QSA,L]

这意味着您无需担心找出请求来自脚本内部的位置(这实际上可能是不可能的,我不确定)。相反,您可以将其作为普通参数读取(它也可以像普通参数一样被破解;请务必对其进行清理)。

于 2013-03-20T22:51:45.127 回答