2016-08-02 37 views
0

的網址:通配符的.htaccess重寫subdomian到子目錄與子域名作爲GET變量

http://username1.example.com/inventory/ 
http://username2.example.com/profile/about 
http://example.com/products/category/ 

改寫爲:

http://example.com/subdomains/inventory/?user=username1 
http://example.com/subdomains/profile/about?user=username2 
http://example.com/products/category/ 

我想我已經搜索互聯網的大部分這一點。

如何通過在域之後立即添加「subdomains」文件夾來重寫路徑,並將子域作爲GET變量通過PHP訪問它?用戶在此地址欄中可以看到的url不應有任何更改。 沒有子域時,不應該有任何更改或重寫。

現在,我可以獲得路徑和子域作爲下面的GET變量,但這不是我想要的。

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$ 
RewriteCond %{REQUEST_URI} !^/index\.php$ 
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC] 
RewriteRule ^(.*)$ /index.php?subdomain=%1&request=$1 [L] 
</IfModule> 

請認爲我是.htaccess noob。

回答

0

試試這個(如果你已經有一些其他的重寫規則,把這些放在上面):

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$ 
RewriteRule^http://example.com/subdomains%{REQUEST_URI}?user=%1 [QSA,L,P] 
+0

上面的代碼重寫通過'http:// abc.example.com'到'HTTP:// example.com/subdomains/subdomains/?用戶= abc'。我不確定是什麼導致了額外的'/ subdomains /'。如果我們可以刪除額外的'/ subdomains /',那麼一切都將是完美的。你可以在[link](http://abc.toours.com/)看到實現@DusanBajic –

+0

當我刪除'[p]'標誌時,一切正常,但用戶瀏覽器中的URL也在改變。 @dusan –

+0

在你的兩行代碼中添加下面的代碼來完成這項工作,但我不知道該怎麼做。 'RewriteRule(。*)/ subdomains /(.*)$ 1/$ 2' 是否有一個mod_rewrite的dummys教程? –