htaccess重写如果url扩展名为.php将其删除

时间:2013-10-25 20:38:22

标签: php regex apache .htaccess mod-rewrite

我还希望301重定向任何以.php结尾的网址。然后,我在内部将/中以/结尾的所有内容重定向到.php.htaccess之前的名称。这些都是独一无二的。但是将它们放在同一个The page isn't redirecting properly我得到RewriteRule ^(.*)\.php$ /$1/ [R=301,L] RewriteRule ^(.*)/$ $1.php [QSA,L]

我在这里做错了什么?

{{1}}

1 个答案:

答案 0 :(得分:5)

这些规则适合您:

RewriteEngine On

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

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

# To internally forward /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]