Forum Moderators: phranque

Message Too Old, No Replies

Simple Mod Rewrite

         

WakeCow

3:30 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



Sorry I'm new to the whole mod_rewrite thing but I'm having trouble with a simple rewrite any help would be much appreciated.

my old url mydomain.com/oldurl/productname/id/
my new url mydomain.com/newurl/productname/id/

here is what I have:

RewriteRule ^/oldurl/(.*)/(.*)/$ /newurl/$1/$2/ [R=301,L]

g1smd

3:35 pm on Mar 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This isn't a rewrite. It's a redirect.

The redirect target should include domain name and protocol.

Change the .* pattern to be something more selective, as those are very inefficient needing hundreds of trial matches for each request. Something like
([^/]+)
might be better.

RewriteRule cannot see the leading / of URL requests when used in .htaccess so you'll need to remove it.

WakeCow

4:20 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



This one worked
RewriteRule ^oldurl/([a-z0-9]+)/([0-9]+)/$ http:\\%{HTTP_HOST}/newurl/$1/$2/ [NC,R=301,L]

g1smd

4:37 pm on Mar 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That should work, but it will redirect all non-www requests to non-www and all www requests to www.

If you have an additional domain canonicalisation rule for redirecting non-www to www, the effect will be a double redirect for non-www old URLs. This redirection chain should be avoided.

It is often best to specify the actual domain name in the rule, rather than use the %{HTTP_HOST} placeholder.

There's related discussion in several threads linked from: [webmasterworld.com...]

Since this is a redirect, you can also make the trailing slash of the original request optional by adding a question mark to the pattern. In that way, requests for both
example.com/oldurl/1234/456
and
example.com/oldurl/1234/456/
will be redirected to the new URL.