【问题标题】:htaccess 301 redirect issue with url variablesurl变量的htaccess 301重定向问题
【发布时间】:2012-12-08 22:56:04
【问题描述】:

如果我使用这个代码,它是成功的:

Redirect 301 /products.php http://website.com.au/product_123.php

但如果我这样做,它不是:

Redirect 301 /products.php?id=123 http://website.com.au/product_123.php

注意 url 中的变量是导致它失败的原因。

我做错了什么?还有其他方法可以做到这一点吗?我真的需要 urls 变量。

【问题讨论】:

    标签: .htaccess http-status-code-301


    【解决方案1】:

    您不能将查询字符串参数放在 Redirect 指令的源 URI 路径中。为此,您必须使用 mod_rewrite 的 %{QUERY_STRING} 变量:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^id=123$
    RewriteRule ^/?product\.php$ http://website.com.au/product_123.php? [L,R=301]
    

    或者说得更笼统:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^id=([^&]+)
    RewriteRule ^/?product\.php$ http://website.com.au/product_%1.php? [L,R=301]
    

    【讨论】:

    • 完美,我不知道 301 对 htaccess 文件的限制
    • 其实乔恩,我还有一个问题。我还需要 /products.php?id=123&a=b 作为链接,但我只需要网址的第一位(id=123),我想忽略其余的 - 这可能吗?我会改变什么
    • 我猜想在 id=123 之后谈论通配符
    • @cardi777 对于第一条规则,您可以将条件的正则表达式更改为 ^id=123($|&),它应该完全匹配 id=123 并忽略任何其他查询字符串参数
    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2011-11-01
    • 2012-08-22
    • 1970-01-01
    相关资源
    最近更新 更多