我的.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]移到底部,希望上面的规则将首先处理,但这不起作用。
我可以做些什么来让它在我需要的时候工作?
发布于 2019-03-07 00:28:39
首先,如果有像redirect这样的mod_alias和像RewriteRule这样的mod_rewrite,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是否像/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html一样以/something/something/something/something.html的形式出现,然后将其重定向到/something/something/something中,如果不是以这种方式出现,则将应用第二个规则。
注意:清除浏览器缓存,然后进行测试
https://stackoverflow.com/questions/55024693
复制相似问题