【问题标题】:htaccess redirect url with parametershtaccess 带参数的重定向 url
【发布时间】:2014-04-29 07:52:09
【问题描述】:

我有数百个这样的链接:

http://www.domain.com/index.php?tag=value

我想将所有链接重定向到

http://www.domain.com/value/

例子:

链接1http://www.domain.com/index.php?tag=LW1fdX49tR 重定向到:http://www.domain.com/LW1fdX49tR/

链接2http://www.domain.com/index.php?tag=A3kh0QLIrc 重定向到:http://www.domain.com/A3kh0QLIrc/

链接3http://www.domain.com/index.php?tag=vXwNR4U9qY 重定向到:http://www.domain.com/vXwNR4U9qY/

我该怎么做? 谢谢!

【问题讨论】:

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


    【解决方案1】:

    除了重定向请求之外,您可能还想确保新 url 也确实有效。为此,您需要外部重定向和内部重写。在下面的示例中,我使用 THE_REQUEST 技巧仅在它是实际请求 url 时触发规则,而不是在内部重写时触发规则。需要防止死循环。

    #External redirect with THE_REQUEST trick; change R to R=301 when everything works correctly
    RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?tag=(.*)\ HTTP
    RewriteRule ^ /%2? [R,L]
    
    #Internal rewrite
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?tag=$1 [L]
    

    【讨论】:

    • 新链接看起来像:http://www.domain.com/value.php?tag=value 我需要是http://www.domain.com/value/
    • 是的,而且?你有什么问题需要调整答案才能适用于这种情况?
    • 你能看看my question这里吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    相关资源
    最近更新 更多