2016-04-15 105 views
1

我需要301将所有以.php结尾的URL重定向到非扩展版本。但重写规则需要与现有的重写规则配合使用。htaccess从多个正斜杠的URL中删除.php扩展

# ==== 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] 

这是此题的延伸here

所以期望的结果是:

domain.com/region/brand/more-content-here.php 

永久重定向到:

domain.com/region/brand/more-content-here 

但获取实际的文件在:

domain.com/region-brand-more-content-here.php 

尝试了一些不同的想法,但他们没似乎不符合现有规则,而htaccess并不是我的力量。也需要它是查询字符串友好。任何帮助,将不胜感激。

回答

1

要删除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] 
+1

完美!谢谢。我所需要的只是第一步,因为#2已经在现有规则中涵盖了。 – Dodo

1

工作代码:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
相关问题