使用 URL 中的查询字符串参数重定向 URL

发布于 2024-10-08 15:58:53 字数 1203 浏览 2 评论 0原文

我有一堆来自最近更新为 EE2 的旧网站的 URL。这些 URL 如下所示:
http://www.example.com/old/path.asp?dir=Folder_Name

这些 URL 需要重定向到:
http://www.example.com/new_path/folders/folder_name

并非全部Folder_Name 字符串与 folder_name URL 段匹配,因此这些很可能需要静态重定向。

我对名为“Example_One”的特定文件夹尝试了以下规则,该文件夹映射到新站点上名为“example1”的页面:

Redirect 301 /old/path.asp?dir=Example_One
             http://www.example.com/new_path/folders/example1

但这不起作用。我没有重定向,只是收到 404 错误,告诉我 http://www.example找不到 .com/old/path.asp?dir=Example_One

编辑:
这里还有一个可能相关也可能不相关的次要问题:我有一个如下所示的包罗万象的规则:
重定向 301 /old/path.asp http://www.example.com/new_path

使用规则,类似于上面第一个请求的请求将被重定向到:
http://www.example.com/new_path?dir=Folder_Name< /code> 会触发 404 错误。

I have a bunch of URLs from an old site that I recently updated to EE2. These URLs look like this:
http://www.example.com/old/path.asp?dir=Folder_Name

These URLs will need to redirect to:
http://www.example.com/new_path/folders/folder_name

Not all Folder_Name strings match up with folder_name URL segments, so these will most likely need to be static redirects.

I tried the following Rule for a particular folder called "Example_One" which maps to a page on the new site called "example1":

Redirect 301 /old/path.asp?dir=Example_One
             http://www.example.com/new_path/folders/example1

But this doesn't work. Instead of redirecting I just get a 404 error telling me that http://www.example.com/old/path.asp?dir=Example_One cannot be found.

EDIT:
There's a secondary problem here too which may or may not be related: I have a catch-all rule that looks like this:
redirect 301 /old/path.asp http://www.example.com/new_path

Using the rule, requests like the first one above will be redirected to:
http://www.example.com/new_path?dir=Folder_Namewhich triggers a 404 error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

奶茶白久 2024-10-15 15:58:53

只需要更多地搜索 Google 即可找到 mod_rewrite 的正确语法。

使用上面的示例:

RewriteCond %{QUERY_STRING} ^dir=Example_One$
RewriteRule ^/old/path\.asp$ /new_path/folders/example1? [R=301,L] 

这解决了上面的两个问题 - 至于为什么 EE 对查询字符串中的一个参数进行 404 处理,这个问题仍然没有解决,但这可以作为该问题的解决方法。

您还可以将 URL 重定向到特定页面,其中参数每次可能具有不同的值。其中一个示例是 Google UTM Campaign 跟踪(在这种情况下,跟踪查询字符串会触发 404):

链接:http://www.example.com/?utm_source=xxx&.....(触发404)
应重定向到: http://www.example.com

RewriteCond %{QUERY_STRING} ^utm_source=
RewriteRule ^$ http://www.example.com? [R=301,L] 

注意:这只会将这些请求重定向到主页,由 ^$ 定义。如果要将 utm 请求重定向到其他页面,则需要更改该 RewriteRule 的第一部分。

Just had to scour Google a bit more to find the proper syntax for mod_rewrite.

Using the example from above:

RewriteCond %{QUERY_STRING} ^dir=Example_One$
RewriteRule ^/old/path\.asp$ /new_path/folders/example1? [R=301,L] 

This fixes both of the problems above -- as to why EE is 404ing with one parameter in the Query String, that problem is still unsolved, but this works as a workaround to that problem.

You can also redirect URLs to a specific page where the parameter may have a different value each time. One example of this is Google UTM Campaign tracking (in situations like this where the tracking query string triggers a 404):

Link: http://www.example.com/?utm_source=xxx&..... (triggers 404)
Should Redirect to: http://www.example.com

RewriteCond %{QUERY_STRING} ^utm_source=
RewriteRule ^$ http://www.example.com? [R=301,L] 

Note: This will only redirect those requests to the homepage, as defined by ^$. If you want to redirect utm requests to a different page, you'll need to change the first part of that RewriteRule.

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