2011-07-23 122 views
0

我剛剛開始使用的.htaccess mod_rewrite我發現自己難倒:如何將index.php重定向到index.php?route = home,並將其重寫爲/ home?

我建立基於查詢字符串加載意見後index.php的應用程序(使用MVC模式);例如。 domain.com/index.php?route=home將加載主頁。

然而,這意味着domain.com/index.php沒有查詢字符串加載任何東西......或更多的加載點,domain.com加載什麼。

。 。 。

我想什麼,是domain.com(和domain.com/index.php

重定向到domain.com/index.php?route=home

,然後有domain.com/index.php?route=home改寫爲domain.com/home

任何幫助,將不勝感激!謝謝。

回答

3

這裏是學習如何有效地使用.htaccess

Tips and Tricks

RewriteRule ^(.*) index.php?route=$1 [L] 

所以這個環節的好去處:

http://www.example.com/home 

會重定向到

http://www.example.com/index.php?route=home 

UPDATE:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*) index.php?route=$1 [L] 

使用條件將確保您的CSS和JavaScript犯規打破

編輯:由於通過Shango評論,我改變/^(.*)/^(.*)

+0

我試過'RewriteEngine On'然後'RewriteRule /^(.*)/ index.php? route = $ 1 [L]',但在「http:// domain.com/home」中輸入時顯示「在此服務器上未找到請求的URL/home」。我還需要添加一個默認的查詢字符串,而不僅僅是'domain.com/home'重定向。 – jlmakes

+0

用於測試將'[L]'更改爲'[R,L]'並告訴我地址​​欄上的網址是什麼 – Ibu

+1

RewriteRule ^(。*)index.php?route = $ 1 [L,QSA] this should追加查詢字符串 –

1

試試這個:

RewriteEngine ON 

RewriteCond %{REQUEST_URI} !^/(.*)/(.+) 
RewriteRule ^([^/]*) index.php?route=$1 [L,QSA] 
+0

抱歉沒有注意到第一個答案已更新,也許第一個答案更新應該更好。只是QSA –

+0

工程很棒。你的增加是獲得這個工作的關鍵。 – jlmakes

相關問題