Forum Moderators: phranque

Message Too Old, No Replies

301 redirect with dynamic pages

Moved from one system to another, variable in URL

         

Diemux

7:11 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



I'm having trouble using a 301 redirect rule to redirect traffic from one URL to another:


Old url: domain.com/submap/submap/edd.php?op=view&id=743

New url: domain.com/submap/submap/view.php?item_id=743

How should the 301 redirect look like? I can't get it to work. I tried below code with no success:


Redirect 301 ^/submap/submap/edd.php?op=view&id=(.*)$ http://www.domain.com/submapsubmap/view.php?item_id=$1

sublime1

7:44 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



Hi Diemux --

While Redirect is a valid option, the more common practice these days is to use RewriteRule.

[httpd.apache.org...]

The RewriteRule knows only about the "path" of the URI, not the query string, so you'll need some trickery with RewriteCond like this:


RewriteCond ${REQUEST_URI} ^/submap/submap/edd.php?op=view&id=(.*)$
RewriteRule .* http://www.domain.com/submap/submap/view.php?item_id=%1 [R=301,L]


Not that the back-reference in this case is to the captured element in the URI in the RewriteCond (the value after "id=", in parenthesis), and therefore you need to use a %1 rather than the $1.

Note, there's a flag "query string append" or [QSA] that does not apply here: you are replacing the query string, not changing it.

I think this is right :-)

Tom

jdMorgan

8:08 pm on Oct 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The mod_alias Redirect directive can't see the query string at all, which is why we want to use mod_rewrite instead:

RewriteCond %{QUERY_STRING} ^op=view&id=(.*)$
RewriteRule ^/submap/submap/edd\.php$ http://www.domain.com/submap/submap/view.php?item_id=%1 [R=301,L]

This code is for use in a server config file outside of any <Directory> section. For use within a <Directory section> or for use in .htaccess, remove the leading slash from the RewriteRule pattern.

Jim

Diemux

8:18 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



Hello Tom and Jim,

Thank you for the quick responses. The first code given by Tom didn't work, traffic was redirected but the string wasn't send along.

The second code by Jim worked perfectly, thank you!