[英]PHP/.htaccess: removing php extension from url
我使用.htaccess代碼刪除所有網頁的.php擴展名。 這是我使用的代碼:
RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
它似乎不起作用。 我想我錯過了什么。 當我輸入www.mysite.com/about/來獲取www.mysite.com/about.php時,它會返回錯誤404(找不到頁面)。 有人可以請一些亮點。
謝謝,保羅G.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
# uncomment the below rule if you want the "/" to be required
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]
以上規則將:
/
如果存在之前出現的文件名 所以它適用於所有這些例子:
http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact
如果你想要你可以刪除?
和注釋規則一樣,只允許它接受以/
結尾的URL。
http://domain.com/about/
http://domain.com/contact/
現在這些是上述工作的重要一步:
.htaccess
,例如/home/youraccount/public_html/.htaccess
-MultiViews
.htaccess
所在的同一位置,例如在您的情況下是about.php
文件 似乎規則末尾的斜線可能存在,或者可能不存在。 添加一個?
使它成為可選項,以便mysite.com/about
和mysite.com/about/
都匹配。
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-\s]+)/?$ /$1.php
很難說這是否是導致你的問題的原因,或者是否是其他東西。 mysite.com/about.php
也會給你一個錯誤嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.