我試圖.php從鏈接中隱藏檔案的擴展名
例如www.example.com/about.php
要顯示 www.example.com/about
我做了什么
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
它完美地作業。
但我有另一個鏈接,example.com/news.php?id=45
根據上述規則,我可以訪問該鏈接
example.com/news?id=45 without .php
但我想隱藏id=45我想要這樣example.com/news/45
我做了什么 RewriteRule ^news.php?id=([0-9] ) /news/$1 [NC,L]
但它不起作用我收到 500 內部服務器錯誤
uj5u.com熱心網友回復:
試試這樣:
# MultiViews must be disabled for "/news/45" to work
Options -MultiViews
RewriteEngine on
# Rewrite "/news/45" to "news.php?id=45"
RewriteRule ^news/(\d )$ news.php?id=$1 [L]
# Handle extensionless ".php" URLs
RewriteCond %{REQUEST_URI} !\.\w{2,3}$
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
您的 500 Internal Server Error 實際上是由“隱藏”.php擴展的原始指令引起的,而不是您的“新”指令(實際上并沒有做任何事情)。您的原始指令會重寫/news/45對/news/45.phpto/news/45.php.php等的請求,從而創建重寫回圈(500 錯誤)。
請參閱我對 ServerFault 上以下問題的回答,并詳細解釋了此行為:https : //serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a -500-錯誤
我做了什么
RewriteRule ^news.php?id=([0-9] ) /news/$1 [NC,L]但它不起作用我收到 500 內部服務器錯誤
該指令的邏輯是顛倒的,并且永遠不會真正匹配請求的 URL(或任何與此相關的內容),因此實際上不會做任何事情。然而,它在語法上沒問題,所以不會觸發錯誤。
500 錯誤可能只是通過請求/news/45(使用您的原始指令)而發生,無論該指令是否到位。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/365764.html
