RewriteCond%{HTTP_HOST}没有响应

时间:2014-07-10 08:15:33

标签: php apache .htaccess mod-rewrite url-rewriting

我想根据子域名重写网址。但是当转到http://test2.domain.com/test/时,它会转到http://test1.domain.com/test/。当我测试时,我发现当我删除第一个RewriteRule行(test1.domain.com部分)(RewriteRule ^ test /&(。*)$ index.php?menuID = 1& $ 1 [L])时工作。出了什么问题?

###start_test1.domain.com###
RewriteCond %{HTTP_HOST} ^(test1.)?domain.com [NC]
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^test/&(.*)$ index.php?menuID=1&$1 [L]
RewriteRule ^test/ index.php?menuID=1[L]
###end_test1.domain.com###


###start_test2.domain.com###
RewriteCond %{HTTP_HOST} ^(test2.)?domain.com [NC]
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^test/&(.*)$ index.php?menuID=2&$1 [L]
RewriteRule ^test/ index.php?menuID=2[L]
###end_test2.domain.com###

1 个答案:

答案 0 :(得分:1)

RewriteCond仅适用于下一个RewriteRule。您的正则表达式^(test2.)?domain.com看起来也不正确。

你的规则如下:

RewriteEngine On

###start_test1.domain.com###
RewriteCond %{HTTP_HOST} ^test1\.domain\.com$ [NC]
RewriteRule ^test/&(.*)$ index.php?menuID=1&$1 [L,B]

RewriteCond %{HTTP_HOST} ^test1\.domain\.com$ [NC]
RewriteRule ^test/ index.php?menuID=1[L]
###end_test1.domain.com###

###start_test2.domain.com###
RewriteCond %{HTTP_HOST} ^test2\.domain\.com$ [NC]
RewriteRule ^test/&(.*)$ index.php?menuID=2&$1 [L,B]

RewriteCond %{HTTP_HOST} ^test2\.domain\.com$ [NC]
RewriteRule ^test/ index.php?menuID=2[L]
###end_test2.domain.com###