【问题标题】:htaccess redirect all pages ending with .html to without html Except some pageshtaccess 将所有以 .html 结尾的页面重定向到没有 html 的页面 除了某些页面
【发布时间】:2019-03-07 00:28:39
【问题描述】:

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

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

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    首先,如果有 mod_aliasredirectmod_rewriteRewriteRule 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 的这种形式出现,如/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html 然后将其重定向到/something/something/something,如果不是以这种方式出现,将应用第二条规则。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-08
      • 2012-10-29
      • 1970-01-01
      • 2012-08-09
      • 2014-05-08
      • 2011-11-04
      • 2012-01-02
      • 2016-07-20
      相关资源
      最近更新 更多