以下示例解释了我的问题。
这是我的.htaccess代码
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /user.php?user=%1
RewriteRule ^user\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /user.php?path=$1 [L,QSA]
预期:
http://example.com > /index.php
http://example.com/contact > /index.php?path=contact
http://example.com/cat/sub1/sub2 > /index.php?path=cat/sub1/sub2
http://jack.example.com > /user.php?user=jack
http://jack.example.com/contact > /user.php?user=jack&path=contact
http://jack.example.com/cat/sub1/sub2 > /user.php?user=jack&path=cat/sub1/sub2
发生的经过:
http://example.com > OK
http://example.com/contact > It opens user.php?path=contact
http://example.com/cat/sub1/sub2 > It opens user.php?path=cat/sub1/sub2
http://jack.example.com > OK
http://jack.example.com/contact > OK
http://jack.example.com/cat/sub1/sub2 > OK
我尝试了很多方法,但没有解决它。有什么帮助吗?
答案 0 :(得分:1)
由于您使用的是同一个主域,我只需将其包含在规则中而不是通配符中。试一试。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} ((?!www).+)\.example\.com [NC]
RewriteRule ^$ /user.php?user=%1 [L]
RewriteCond %{HTTP_HOST} ((?!www).+)\.example\.com [NC]
RewriteRule ^(.+)$ /user.php?user=%1&path=$1 [L]
RewriteRule ^user\.php$ - [L]
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]