开发者

Redirect URL using Query String parameter in URL

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 lik开发者_StackOverflow社区ely 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜