【问题标题】:.htaccess 301 redirect with regex.htaccess 301 使用正则表达式重定向
【发布时间】:2012-06-21 20:29:47
【问题描述】:

我需要更新我的 .htaccess 文件以将所有 URL 从 http://example.com/pages/5604/article/something/?page=299 永久重定向到 http://example.com/pages/5604/article/。这是我的实现(遗憾的是它不起作用):

RewriteRule ^pages/(.*)/(.*)/something/\?page=(.*) /pages/$1/$2 [R=301,L]

提前感谢您的帮助和建议!

【问题讨论】:

    标签: regex .htaccess mod-rewrite redirect apache2


    【解决方案1】:

    QUERY_STRING 不在匹配的 url 中,您必须在 RewriteCond 中单独匹配。类似(未经测试):

    RewriteCond %{QUERY_STRING} ^page=(.*) 
    # Apache >=  2.4
    RewriteRule ^pages/(.*)/(.*)/something/ /pages/$1/$2 [R=301,L,QSD]
    # Apache <  2.4 
    RewriteRule ^pages/(.*)/(.*)/something/ /pages/$1/$2? [R=301,L]
    

    如果您需要RewriteRulepage= 之后的值,您可以像使用$1 一样使用%1RewriteCond 中的匹配项是%N,在RewriteRule $N)

    【讨论】:

    • 这是一个很好的开始(谢谢!),但它仍然附加?page=(.*)。我怎样才能摆脱它呢?
    • 嗯,这对我来说是新的,但 QSD flag 似乎是必需的,今天也学到了一些东西 ;)
    • 哦,如果您使用的是 ?。
    猜你喜欢
    • 2014-12-05
    • 2018-05-31
    • 2018-06-17
    • 2017-06-21
    • 2015-01-30
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多