url 变量的 htaccess 301 重定向问题 [英] htaccess 301 redirect issue with url variables

查看:33
本文介绍了url 变量的 htaccess 301 重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用这个代码,它就成功了:

If I use this code, it's successful:

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

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

But if I do this, it isn't:

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

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

Note the variable in the url is what's causing it to fail.

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

What am I doing wrong? Is there another way to do this? I really need the urls variables.

推荐答案

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

You can't put query string parameters in the source URI path of the Redirect directive. You'll have to use mod_rewrite's %{QUERY_STRING} variable for that:

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]

这篇关于url 变量的 htaccess 301 重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆