[英]Why won't this simple RewriteRule work?
這應該很簡單,但我無法弄清楚。 我有這個非常簡單的mod_rewrite規則,它只是不想工作。 這是.htaccess文件的全部內容。
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。 我嘗試了多個URL:
在所有情況下,PHP都將'page'設置為“ index.php”。 這不是index.php的問題,因為我用簡單的腳本替換了index.php的全部內容,但該腳本仍簡單地回顯了'page'的值,但仍以index.php的形式出現。
我真的錯在這里錯了,任何幫助都太好了!
謝謝
阿德里安
不能使用任何簡單原因的任何原因,例如:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
如果您試圖防止現有頁面被重寫,則!-f
將解決這一問題( if file does not exist
而if directory does not exist
,則re-write
)
我認為這是您想要的。 不幸的是,如果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參數,然后在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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.