我嘗試了很多方法,但都沒有奏效。Blog 是域的名稱,Home 是域中的一個檔案夾,而 post.php 是從資料庫獲取詳細資訊的頁面。所以,在我的域中,我有:home/post.php
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-] )$ /post?read=$1 [L] # Handle page requests
以上是我使用的最后一個代碼,它不起作用。我收到 404 錯誤。
uj5u.com熱心網友回復:
RewriteEngine On # Turn on the rewriting engine RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([A-Za-z0-9_-] )$ /post?read=$1 [L] # Handle page requests.htaccess 位于檔案夾的根目錄中。
您似乎忽略了home子目錄。(盡管您在問題中都提到了home和Home- 請注意這是區分大小寫的。)如果“/post是 PHP 檔案”,那么您可能應該重寫為post.php,而不僅僅是post?
請注意,Apache 不支持行尾注釋,正如您在此處使用的那樣。(充其量它們會被忽略,最壞的情況它們會觸發 500 錯誤。)
如果.htaccess檔案位于檔案根目錄中,如您所建議的,那么您需要撰寫如下規則:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/([\w-] )$ home/post.php?read=$1 [L]
\w速記字符類與[A-Za-z0-9_].
該RewriteRule 模式匹配相對于.htaccess檔案位置的 URL 路徑。
另一方面,如果.htaccess檔案位于/home子目錄中(即 at /home/.htaccess),那么您將改為撰寫如下規則:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-] )$ post.php?read=$1 [L]
請注意,替換字串上沒有斜杠前綴。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425157.html
上一篇:Apachemod_rewrite-不需要的重定向而不是重寫
下一篇:變數URL的永久重定向
