.htaccess在删除php扩展名和内部重定向时抛出文件夹外

时间:2018-07-10 10:48:54

标签: .htaccess redirect mod-rewrite

我正在根据这个问题工作 (Redirect .php urls to urls without extension) 但这会将我发送到根文件夹。

RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

例如我的原始网址http://www.website.com/products/abc.php
重定向后,它会将我发送到http://www.website.com/abc

1 个答案:

答案 0 :(得分:1)

这是可以处理子文件夹的通用规则

Options -MultiViews
RewriteEngine On

# browser requests PHP
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+/)?([^/\s\?&]+)\.php
RewriteRule ^ /%1%2 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ /$1.php [L]