使用htaccess文件删除文件扩展名.php

时间:2012-05-22 17:07:22

标签: .htaccess

我想通过htaccess文件从网址中删除.php。例如home.php到家 我在htaccess文件中使用以下重写规则。

    RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

我还想将登录名指定为索引。 我怎样才能改变它。

2 个答案:

答案 0 :(得分:2)

这是您可以用来隐藏.php扩展名的代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L,NC]

答案 1 :(得分:0)

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp

在URL的末尾添加尾部斜杠 (例如:http://www.example.com/about-us/转到http://www.example.com/about-us.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html 

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]