htaccess 将所有以 .html 结尾的页面重定向到不带 html 除了某些页面



我的重定向.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]移到底部,希望上面的规则将首先处理,但这不起作用。

我能做些什么来根据需要完成这项工作?

首先,如果有像redirect这样的mod_alias和像RewriteRulemod_rewrite规则这样的mod_rewrite,则在mod_alias规则之前执行。

有许多senarios可以解决这个问题,例如从主重写规则中排除所有其他重定向规则,但它认为如果其他规则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就像/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html然后将其重定向到/something/something/something,如果不以这种方式出现,则将应用第二条规则。

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

最新更新