繁体   English   中英

htaccess将所有以.html结尾的页面重定向到不包含html的页面,但某些页面除外

[英]htaccess redirect all pages ending with .html to without html Except some pages

我的.htaccess重定向规则有问题。

我需要将以.html结尾的页面重定向到以斜杠结尾的页面。
/about-us.html/about-us/

但是也有一些页面需要重定向到不带.html特定页面,例如
/gallery/first-gallery/this-is-the-gallery.html/gallery/new/

问题是我的规则不能很好地协同工作,似乎重定向.html页面的规则忽略了所有其他规则。

这就是我的.htaccess样子

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?(.*).(html)$ /$1 [R=301,L]
Redirect 301 /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/
Redirect 301 /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131174-58e2a063b8b06princeton-texas-roofing-services-2.html /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/
#Many other 301 rules below
</IfModule>

所以当我输入
domain.com/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html
在浏览器中,它重定向到
domain.com/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1
我需要这个特定的重定向来重定向到
/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/

我尝试将RewriteRule ^/?(.*).(html)$ /$1 [R=301,L]移到底部,希望上面的规则将首先处理,但这没有用。

我可以根据需要做些什么?

首先,如果有mod_aliasredirectmod_rewrite类似RewriteRule mod_rewrite规则之前得到执行mod_alias的规则。

有很多解决该问题的方法,例如从主重写器中排除所有其他重定向规则,但是它认为如果其他规则redirect具有相同的目标以仅省略URI的最后一部分,则可以制定以下规则:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([^\/]+)/([^\/]+)/([^\/]+)/([^\/]+)\.(html)$ /$1/$2/$3 [R=301,L]
RewriteRule ^(.*)\.(html)$ /$1 [R=301,L]
</IfModule>

上面的规则将检查URI是否以/something/something/something/something.html格式/something/something/something/something.html例如/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html然后将其重定向到/something/something/something ,如果不是这样,则将应用第二条规则。

注意:清除浏览器缓存然后进行测试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM