[英]RewriteRule to match all characters
因此,我正在嘗試編寫一個htaccess文件,該文件將使用在其后添加的查詢將所有內容重寫回根索引文件。
example.com/cheese/
將路由到example.com/index.php?cheese/
example.com/cheese
將路由到example.com/index.php?cheese
example.com/bob/fred
將轉為example.com/index.php?bob/fred
進入盡可能多的層次,有人有建議嗎?
目前正在嘗試此操作,但無法正常工作:
RewriteEngine on
RewriteRule ^(.*)/?$ index.php?$1
我知道它不起作用,因為我正在使用以下命令對其進行測試,並且查詢輸出不正確。
<?php
$page = $_SERVER['QUERY_STRING'];
echo $page;
?>
您可以在DOCUMENT_ROOT/.htaccess
文件中使用此規則:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
將避免循環,並跳過有效的文件(如css,js圖像)重寫為index.php
。
通過以下方式訪問查詢字符串:
$_SERVER["QUERY_STRING"]
首先,必須啟用mod_rewrite
。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule /(.*) index.php?$1
</IfModule>
但是,如果需要使用$_SERVER['QUERY_STRING']
我建議您使用:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule . index.php
</IfModule>
然后從$_SERVER['REQUEST_URI']
檢索url,因為您的解決方案中斷了每個GET請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.