.htaccess捕获所有子域&;GET作为GET变量


.htaccess catch all subdomains & GET as GET variable

下面的例子解释了我的问题。

这是我的.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

我试了很多方法都没有解决。有什么帮助吗?

由于您使用的是同一个主域,所以我只将其包含在规则中,而不是通配符中。试试看。

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]