【问题标题】:Redirect URL if it contains at least 5 segments and the first segment is blog如果它包含至少 5 个段并且第一个段是博客,则重定向 URL
【发布时间】:2016-02-22 22:16:20
【问题描述】:

我想重定向所有以 blog 作为分段 1 的 URL,如果至少有 5 个分段。

例如:domain.com/blog/entry/title/2012/P5 将重定向到 domain.com/blog

domain.com/blog/entry/2013 不会重定向,因为它没有至少 5 个段。

domain.com/blog/entry/2013/P5/title/howdy/test 将重定向到 domain.com/blog,因为它至少有 5 个段。

【问题讨论】:

    标签: regex apache .htaccess url-redirection


    【解决方案1】:

    您可以在 /blog/.htaccess 顶部使用此规则(如果不存在则创建它):

    RewriteEngine On
    
    RewriteRule ^(?:[^/]+/){3}[^/]+(?:/.*)?$ /blog [L,R=302]
    

    如果/blog 不是真实目录,则在站点根目录.htaccess 中使用此规则:

    RewriteEngine On
    
    RewriteRule ^(blog)/(?:[^/]+/){3}[^/]+(?:/.*)?$ /$1 [NC,L,R=302]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 2011-03-14
      • 2013-08-11
      • 2020-04-08
      • 2016-03-23
      相关资源
      最近更新 更多