重定向到新域并使用 htaccess 在地址中添加变量

发布于 2024-10-21 16:07:51 字数 285 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2024-10-28 16:07:51

好的,从旧域到新域的正常基本重定向将类似于:

RewriteCond %{HTTP_HOST} ^old\.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]  

要添加此额外的 URL 变量,我们需要考虑两种情况。首先,有些页面已经有 URL 变量,在这种情况下我们只想附加 &comingFromOld=1。其次,有些页面没有任何 URL 变量,在这种情况下,我们希望附加 ?comingFromOld=1 。

您可能会期望这可能需要两组规则或一个复杂的正则表达式来允许这两种情况。幸运的是,我们可以使用一个很好的标志,QSA,它将附加原始查询字符串(如果存在)。我想这应该涵盖你。

RewriteCond %{HTTP_HOST} ^old\.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1?comingFromOld=1  [QSA,R=301,L]

因此,您最终会得到类似 http://new.com/index.php?comingFromOld=1http://new.com/index.php?comingFromOld= 的 URL 1&var1=3

Ok, so the normal basic redirect from old to new domains would be something like:

RewriteCond %{HTTP_HOST} ^old\.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]  

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.

RewriteCond %{HTTP_HOST} ^old\.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1?comingFromOld=1  [QSA,R=301,L]

So you'll either end up with URLs like http://new.com/index.php?comingFromOld=1 or http://new.com/index.php?comingFromOld=1&var1=3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文