問題描述
我使用 .htaccess 代碼刪除了所有網(wǎng)頁的 .php 擴(kuò)展名.這是我使用的代碼:
I was using .htaccess code to remove .php extension for all my web pages. Here's the code I use:
RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-s]+)/$ /$1.php
它似乎不起作用.我想我錯過了一些東西.當(dāng)我輸入 www.mysite.com/about/以獲取 www.mysite.com/about.php 時,它返回錯誤 404(未找到頁面).有人可以請說明一下.
It doesn't seem to work. I think I'm missing something. When I type www.mysite.com/about/ to get www.mysite.com/about.php it returns error 404 (page not found). Can someone please shed some light.
謝謝,保羅 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]
以上規(guī)則將:
- 如果文件夾存在則不會重定向
- 如果文件不存在則不會重定向
- 將重定向
/
之前的內(nèi)容,如果存在作為文件名
- will not redirect if a folder exist
- will not redirect if the file does not exist
- will redirect what comes before the
/
if one is present as the file name
所以它適用于所有這些示例:
So it will work for all these examples:
http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact
如果你愿意,你可以刪除 ?
,就像注釋規(guī)則一樣,讓它只接受以 /
結(jié)尾的 URL.
If you want you can remove the ?
, like the commented rule, to make it accept only URL's that end with a /
.
http://domain.com/about/
http://domain.com/contact/
現(xiàn)在這些是上述工作的重要步驟:
Now these are important step for the above to work:
- 它必須進(jìn)入您的根文件夾中的
.htaccess
,例如/home/youraccount/public_html/.htaccess
- 重寫規(guī)則前的選項(xiàng)很重要,特別是
-MultiViews
- 該文件必須與
.htaccess
位于同一位置,例如在您的情況下是about.php
文件 - PHP 必須運(yùn)行正常.
- It must go into the
.htaccess
on your root folder for example/home/youraccount/public_html/.htaccess
- The Options before the rewrite rule are very important specially
-MultiViews
- The file must exist on the same place the
.htaccess
is for example in your case theabout.php
file - The PHP must be working obviously.
這篇關(guān)于PHP/.htaccess:從 url 中刪除 php 擴(kuò)展名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!