0

可能重复:
使用 .htaccess 删除 .php 扩展名

我有

http://www.example.com/test/categoryform.php

在我的 .htaccess 文件中,如何重写它以显示为:

http://www.example.com/test/categoryform/
4

3 回答 3

5

试试这个代码

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 扩展名也很有用

于 2012-08-27T08:43:17.460 回答
1

简单的问题问了一千

RewriteEngine on
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

来源:通过 .htaccess 删除文件扩展名

于 2012-08-27T08:45:03.083 回答
1

为了删除尾部斜杠,您需要在条件中匹配它

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]
于 2012-08-27T08:50:05.237 回答