将通配符子域重定向到子目录,而无需更改地址栏中的 URL [英] Redirect wildcard subdomains to subdirectory, without changing URL in address bar
问题描述
我在这里阅读了很多关于此的问题和答案,但似乎没有一个能解决我的具体问题.
I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.
我想将任何子域重定向到要匹配的子目录.
I want to redirect any subdomain to the subdirectory to match.
所以:x.domain.com 会转到 domain.com/x,而 y.domain.com 会转到 domain.com/y - 但我想在地址栏中不更改 URL 的情况下执行此操作.
So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.
这是我目前所拥有的:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ /%1 [P,L]
但这将我带到一个网站重定向循环,在子域仍然存在的 URL 栏中有一个不正确的地址.
But this takes me to a website redirect loop, with an incorrect address in the URL bar where the subdomain still exists.
例如,x.domain.com 将我带到 x.domain.com/x 并且我收到重定向循环错误.
For example, x.domain.com takes me to x.domain.com/x and I get a redirect loop error.
如果有人能指出我正确的方向,我将不胜感激!我改变的一切似乎都不起作用......
I'd be grateful if anyone can point me in the right direction! Nothing I change seems to work...
推荐答案
首先确保 apache 配置中的 vhost 配置正确,并且 domain.com 的所有子域都在同一个主机配置中(通配符):
First of all, make sure that the vhost in the apache configuration is properly configured and all subdomains of domain.com are in the same host configuration (wildcard):
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
...
您可以使用以下 htaccess 配置进行重定向:
You can get the redirect working with the following htaccess configuration:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,NC,QSA]
现在,如果您打开 asd.domain.com,它应该将您重定向到 domain.com/asd.您仍然会遇到问题,即重定向在 URL 地址栏中可见.为了防止这种情况,在您的服务器上启用 mod_proxy(并加载子模块)并将L"标志与P"标志交换:
Now, if you open asd.domain.com it should redirect you to domain.com/asd. You will still have the problem, that the redirect is visible in the URL address bar. In order to prevent this, enable mod_proxy (and load the submodules) on your server and exchange the "L" flag with the "P" flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]
如果这不起作用,在子域调用时查看 vhost 配置和 error.log 的内容会很有帮助!
If this doesn't work, viewing the vhost configuration and the content of error.log on subdomain calling will be helpful!
参考文献:
.htaccess 将子域重写到目录
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p
References:
.htaccess rewrite subdomain to directory
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p
这篇关于将通配符子域重定向到子目录,而无需更改地址栏中的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!