我有基于domain/foo/bar嵌套 url 導航的簡單 php 應用程式。
例如,我有一個index.php帶有about導航鏈接的主頁,該鏈接應該導航到domain/en/about, whereen并且about必須轉移到 url 引數,如index.php?url=....
但是當我點擊到時,about我找到了404domain/en/about并
沒有找到。
我已將 apache2 虛擬域配置配置為:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
<Directory /var/www/html/domain>
Options -Indexes FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
DocumentRoot /var/www/domain/
ServerName domain.local
ServerAlias www.domain.local
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
并.htaccess歸檔為:
order deny,allow
RewriteEngine On
RewriteBase /
RewriteRule .* index.php?url=$0 [QSA,L]
mod_rewrite對于 apache2 已經啟用。
不知道我錯過了什么。
任何幫助表示贊賞!先感謝您!
uj5u.com熱心網友回復:
<Directory /var/www/html/domain> : DocumentRoot /var/www/domain/
您的<Directory>部分和DocumentRoot指令參考了不同的位置,因此無論您將.htaccess檔案放在哪里,它都不會按預期作業。
然而...
RewriteRule .* index.php?url=$0 [QSA,L]
這個規則并不完全正確,因為它最終會在重寫引擎的第二次通過時重寫自己。如果沒有QSA標志,原始url引數值(包含最初請求的 URL 路徑)將丟失。以上內容最終重寫了對/en/aboutto的請求index.php?url=index.php&url=en/about。幸運的是,您的 PHP 腳本仍然讀取$_GET['url']為en/about. 但您可以檢查$_SERVER['QUERY_STRING'].
(而且,如果您只是在替換字串前面加上一個斜杠,即 URL 路徑,您將得到一個無休止的重寫回圈(500 內部服務器錯誤)。但這也可能是稍后添加額外規則的結果。 )
您應該防止對index.php自身的請求被重寫,您可以通過添加附加規則來做到這一點。例如:
RewriteRule ^index\.php$ - [L]
RewriteRule .* index.php?url=$0 [QSA,L]
但是,這仍然會重寫您的靜態資產(假設您正在鏈接到內部影像、CSS 和 JS 檔案?)。因此,如果請求已經映射到靜態檔案,您通常需要通過附加條件阻止處理規則來防止這種情況發生。
例如:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
CondPattern檢查 TestString 是否映射 到檔案。前綴否定了這一點。因此,僅當請求未映射到檔案時,該條件才成功。-f!
uj5u.com熱心網友回復:
您需要在要捕獲的內容周圍加上括號。反向參考索引以“1”開頭:
RewriteRule (.*) index.php?url=$1 [L,QSA]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442349.html
上一篇:如何計算許多矩形/框的交集
