我需要301将所有以.php结尾的URL重定向到非扩展版本。但是重写规则需要与下面现有的规则配合使用。
# ==== REWRITE URLS ====
RewriteEngine On
# pass through root
RewriteRule ^(index'.php|Sitemap'.xml)?$ - [L]
# no more / so add extension
RewriteCond $1 !/
RewriteCond $1 !'.php$
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_URI} !'.php$
RewriteRule ^(_assets|_css|_fonts|_includes|_scripts)($|/) - [L] #exclude these folders using the 'last: L' flag
RewriteRule ^(.*)/(.*)$ /$1-$2 [L]
这是这个问题的延伸。
所以想要的结果是:
domain.com/region/brand/more-content-here.php
永久重定向到:
domain.com/region/brand/more-content-here
但在以下位置获取实际文件:
domain.com/region-brand-more-content-here.php
尝试了一些不同的想法,但它们似乎不符合现有的规则,htaccess不是我的强项。还需要它对querystring友好。如有任何帮助,我们将不胜感激。
要删除.php扩展,可以使用:
RewriteEngine on
#1)Permanently redirect "foo.php" to "/foo"#
RewriteCond %{THE_REQUEST} /([^.]+)'.php [NC]
RewriteRule ^ /%1 [NE,L,R=301]
#2)Rewrite "/foo" to "/foo.php"#
#this will internally redirect file to file.php#
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
工作代码:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f