重写URL以排除文件扩展名

时间:2015-04-05 02:53:43

标签: apache .htaccess

我想重写我的网址以排除文件扩展名。例如:

http://www.example.com/filename.php

应该成为:

http://www.example.com/filename/

或:

http://www.example.com/filename

(两者都应该被接受)

这样可以提供更清晰的网址。如何以这种方式重写URL?

1 个答案:

答案 0 :(得分:3)

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 / not optional 
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]

这可能就是你所需要的。