.htaccess mod_rewrite扩展名删除

时间:2014-07-29 15:54:57

标签: php apache .htaccess mod-rewrite redirect

使用搜索,我发现此代码删除了我的共享godaddy托管帐户中的所有.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 %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

它工作得很好,但我想这样做,所以它重写index.php只显示基本URL而不是domain.com/index。

任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R,L,NE]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]