2010-08-11 111 views
0

這應該很簡單,但我無法弄清楚。我有這個非常簡單的mod_rewrite規則,它只是不想工作。這是.htaccess文件的全部內容。爲什麼這個簡單的RewriteRule不能工作?

RewriteEngine On 
RewriteRule ^([A-Za-z0-9-_/\.]+)/?$ index.php?page=$1 [L] 

如果我調用URL domain.com/foo,它應該將其重寫爲index.php?page = foo。但是,它將其重寫爲index.php?page = index.php。我曾嘗試多個網址:

  • index.php頁面= foo的
  • 的index.php
  • /富
  • /

在所有情況下,PHP行爲,如果頁面'設置爲「index.php」。這不是index.php的錯,因爲我用一個腳本來替換index.php的全部內容,這個腳本簡單地迴應了'page'的值,它仍然以index.php的形式出現。

真的失去了我要去錯的地方,任何幫助都會很棒!

感謝

阿德里安

回答

2

你不能用簡單的東西,如任何原因:

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

如果你想保持被改寫現有的網頁,然後!-f將照顧(if file does not existif directory does not exist,然後re-write

+0

我其實並沒有想到(避免重寫實際頁面),但這絕對是我需要做的事情!這對我有用,謝謝! – 2010-08-11 03:39:01

0

我覺得這是你在找什麼。不幸的是,如果URL有其他的GET參數(IE,/ some/url/path?page = 1),它並不能很好地工作。

RewriteEngine On 
RewriteRule ^(.*)$ index.php?page=$1 [L] 

更好的主意。

RewriteEngine On 
RewriteRule ^(.*)$ index.php [L,QSA] 

的QSA標誌將轉發GET PARAMS,然後在index.php中使用$_SERVER['REQUEST_URI']parse_url將請求路由。

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

'qsappend|QSA' (query string append) 
This flag forces the rewriting engine to append a query string part 
in the substitution string to the existing one instead of replacing it. 
Use this when you want to add more data to the query string via a rewrite rule. 
+0

暫時我不會有多得到論據,我只需要這個爲現在工作。我試過你給我的東西,它仍然產生相同的結果。 – 2010-08-11 03:35:43

相關問題