2015-12-21 78 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