2015-12-21 34 views
2

以下示例说明了我的问题。.htaccess捕获所有子域&GET作为GET变量

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

顺便说一句我认为你在你的规则中将user.php与index.php混淆了。因为在你的.htaccess中你有两次。 –

回答

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] 
+0

使用这些规则http://example.com/cat/sub1/sub2可以很好地工作,但子域名http://jack.example.com/cat/sub1/sub2不起作用。路径即将变空。 – ozgrozer

+0

哦,我_think_我明白你想要什么。立即尝试更新的规则。 –

+0

上一个更好:)现在所有链接打开index.php。 – ozgrozer