可能重复:
使用 .htaccess 删除 .php 扩展名
我有
http://www.example.com/test/categoryform.php
在我的 .htaccess 文件中,如何重写它以显示为:
http://www.example.com/test/categoryform/
可能重复:
使用 .htaccess 删除 .php 扩展名
我有
http://www.example.com/test/categoryform.php
在我的 .htaccess 文件中,如何重写它以显示为:
http://www.example.com/test/categoryform/
试试这个代码
RewriteEngine On
# turn on the mod_rewrite engine
RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename
以及如何在 .htaccess问题中隐藏 .php 扩展名也很有用
简单的问题问了一千次
RewriteEngine on
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
为了删除尾部斜杠,您需要在条件中匹配它
RewriteEngine On
# make sure it's not a directory or a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# match out the request URI without the trailing slash
RewriteCond %{REQUEST_URI} ^/([^/]+?)/?$
# and see if it exists
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
但是为了让它显示(例如,它显示在浏览器的地址栏中,您需要:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+?)\.php
RewriteRule ^(.+?)\.php$ /$1/ [L,R=301]