重定向到新域并使用 htaccess 在地址中添加变量
我想使用 htaccess 将我的用户从旧域重定向到新域,同时在地址中添加一个变量以指示他们来自旧域,
这是一个示例,
http://old.com/index.php?var1=3 ==> http://new.com/index.php?var1=3&comingFromOld=1
http://old.com/index.php ==> http://new.com/index.php?comingFromOld=1
非常感谢任何帮助
I would like to redirect my users from an old domain to a new one using htaccess but also add a variable in the address to indicate they are coming from the older domain,
here's an exampel
http://old.com/index.php?var1=3 ==> http://new.com/index.php?var1=3&comingFromOld=1
http://old.com/index.php ==> http://new.com/index.php?comingFromOld=1
any help is greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,从旧域到新域的正常基本重定向将类似于:
要添加此额外的 URL 变量,我们需要考虑两种情况。首先,有些页面已经有 URL 变量,在这种情况下我们只想附加 &comingFromOld=1。其次,有些页面没有任何 URL 变量,在这种情况下,我们希望附加 ?comingFromOld=1 。
您可能会期望这可能需要两组规则或一个复杂的正则表达式来允许这两种情况。幸运的是,我们可以使用一个很好的标志,QSA,它将附加原始查询字符串(如果存在)。我想这应该涵盖你。
因此,您最终会得到类似
http://new.com/index.php?comingFromOld=1
或http://new.com/index.php?comingFromOld= 的 URL 1&var1=3
Ok, so the normal basic redirect from old to new domains would be something like:
To add in this extra URL variables, there are two cases we want to consider. Firstly, some pages will already have URL variables, in which case we just want to append &comingFromOld=1. Secondly some pages won't have any URL variables, in which case we want to append with ?comingFromOld=1 instead.
You'd expect this would maybe require either two sets of rules, or one complicated regex to allow for both cases. Fortunately there's a nice flag we can use, QSA, that will append the original query string (if one exists). I think this should cover you.
So you'll either end up with URLs like
http://new.com/index.php?comingFromOld=1
orhttp://new.com/index.php?comingFromOld=1&var1=3