删除友好 URL 的 .php 扩展名(显式编写)

问题描述:

htaccess 删除我网站文件的 .php 扩展名.

htaccess to remove the .php extension of my site's files.

RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L,QSA]

现在,如果我访问我的网站

Now, if I go to my site

www.mysite.com/home

工作正常,它重定向到 home.php 但 URL 仍然友好.

works fine, it redirects to home.php but the URL is still friendly.

但是如果我写这个网址:

But if I write this URL:

www.mysite.com/home.php

服务home.php,网址不友好

The home.php is served, and the URL is not friendly.

我怎样才能避免这种行为?我希望如果用户写的是www.mysite.com/home.php,则在网址栏中显示的网址是www.mysite.com/home

How can I avoid this behavior? I want that if the user writes www.mysite.com/home.php, the URL displayed in the URL bar be www.mysite.com/home

你可以像这样从请求中删除 .php

you can remove .php from requests like so

RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]