代码之家  ›  专栏  ›  技术社区  ›  Martijn Heemels

使用GET参数(如?blah)时,Apache重定向301失败=

  •  18
  • Martijn Heemels  · 技术社区  · 17 年前

    我为一个客户建立了一个新的PHP站点,希望将排名靠前的Google结果从旧站点结构重定向到新站点结构。

    我已经在documentroot的.htaccess中放置了几十个重定向301,虽然有些重定向301可以正常工作,但我与其他一些重定向301存在问题。

    这很好:

    Redirect 301 /nl/flash/banner_new.swf http://www.example.com/actueel/nieuws.html?action=show&f_id=152
    

    这不行!(由于直接跳过重定向,因此导致404):

    Redirect 301 /nl/index.php?mID=24511&subID=0 http://www.example.com/solutions/printsolutions.html
    Redirect 301 /nl/index.php?mID=24512&subID=0 http://www.example.com/support/koppeling-met-omgeving.html
    

    重定向混合在.htaccess文件中,只有带有GET参数的重定向出现故障。

    有解决办法吗?忽略失败的重定向不是客户的选择。谢谢你的想法。

    3 回复  |  直到 17 年前
        1
  •  18
  •   Community Mohan Dere    9 年前

    Gumbo's answer's 推理是正确的,我无法使他的规则起作用。

    RewriteCond %{REQUEST_URI} /nl/index.php$
    RewriteCond %{QUERY_STRING} ^mID=24511&subID=0$
    RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html [L,R=301]
    
        2
  •  14
  •   Martijn Pieters    11 年前

    同意Gumbo和Martijn的答案。。。但是:

    RewriteCond %{REQUEST_URI} ^/nl/index.php$
    

    我也只能让Martijn而不是Gumbo在我的.htaccess文件所在的地方工作。

    此外,如果您不希望在重写时传递参数字符串,则应在URL的末尾添加一个“?”:

    RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html? [L,R=301]
    

    否则,按照Martijn的代码,它会显示“如果您的URL是/nl/index.php?mID=24511&subID=0,则重定向到 http://www.example.com/solutions/printsolutions.html?mID=24511&subID=0 使用301永久重定向标头,不在此URL上处理更多规则”

        3
  •  12
  •   Gumbo    17 年前

    Redirect 仅在URL路径上运行:

    旧的 URL路径

    ? 直到第一个 #

    但是你可以用 mod_rewrite 为此:

    RewriteCond %{QUERY_STRING} ^mID=24511&subID=0$
    RewriteRule ^nl/index\.php$ http://www.example.com/solutions/printsolutions.html [L,R=301]
    RewriteCond %{QUERY_STRING} ^mID=24512&subID=0$
    RewriteRule ^nl/index\.php$ http://www.example.com/support/koppeling-met-omgeving.html [L,R=301]