首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >htaccess将所有以.html结尾的页面重定向到不带html的页面(某些页面除外

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

Stack Overflow用户
提问于 2019-03-06 21:50:22
回答 1查看 126关注 0票数 0

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

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

/about-us.html/about-us/

但是也有一些页面需要重定向到没有.html的特定页面,比如

/gallery/first-gallery/this-is-the-gallery.html/gallery/new/

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

这是我的.htaccess的样子

代码语言:javascript
复制
<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]移到底部,希望上面的规则将首先处理,但这不起作用。

我可以做些什么来让它在我需要的时候工作?

EN

回答 1

Stack Overflow用户

发布于 2019-03-07 00:28:39

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

有许多资深人士可以解决这个问题,比如从主重写规则中排除所有其他重定向规则,但它认为如果其他规则redirect有相同的目标,只省略URI的最后一部分,您可以制定如下规则:

代码语言:javascript
复制
<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中,如果不是以这种方式出现,则将应用第二个规则。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55024693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档