Forum Moderators: phranque

Message Too Old, No Replies

301 redirect exclusion not working

         

mfarah01

12:51 pm on Mar 11, 2010 (gmt 0)

10+ Year Member



Hi

I've moving my site one from one URL to another. My htaccess looks something like this

RewriteEngine on
Redirect 301 /ntour.jsp [oldsite.com...]
Redirect 301 /vtourr.jsp [oldsite.com...]
RewriteEngine on
RewriteCond %{REQUEST_URI} !/m/
RewriteRule ^(.*)$ [newsite.com...] [R=301,QSA,L]

As you can see I have some 301 redirects before the main one, this was due to some previous changes. I'm now trying to redirect everything but files and folder in the /m/ directory.

The redirect works BUT the /m/ folder is not excluded. Please help.

g1smd

7:55 pm on Mar 11, 2010 (gmt 0)

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



Use RewriteRule (instead of Redirect) for all of the rules to be sure of the order they are processed in.

Add the [L] flag to all of the redirects.

I would replace !/m/ with !^/m/ here.

[edited by: g1smd at 8:02 pm (utc) on Mar 11, 2010]

mfarah01

8:01 pm on Mar 11, 2010 (gmt 0)

10+ Year Member



Thanks.So it's only this part I need to change?

Redirect 301 /ntour.jsp [oldsite.com...]
Redirect 301 /vtourr.jsp [oldsite.com...]

mfarah01

8:02 pm on Mar 11, 2010 (gmt 0)

10+ Year Member



Could you please give me an example, I dont know much about rewrite rules

g1smd

8:04 pm on Mar 11, 2010 (gmt 0)

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



There's more than 10 000 prior examples in this forum, as well as the resources cited in the forum charter.

You've already used a RewriteRule with [R=301,L]. The new code is going to be very similar, but redirecting for specific URL requests not the .* 'all' pattern.

mfarah01

6:18 am on Mar 12, 2010 (gmt 0)

10+ Year Member



I edited the code as follows

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/m/(/?$|/.*$)
RewriteRule ^(.*)$ [newdomain.com...] [R=301,QSA,L]

Now when I type in www.olddomain.com/m/ the redirect is excluded. However as soon as I click on any link in the /m/ folder to another file in the /m/ folder, I get a 404 since it's trying to open the page on the new domain.

mfarah01

6:31 am on Mar 12, 2010 (gmt 0)

10+ Year Member



OK I think I have it, I changed it as follows;

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^(.*)$ [newdomain.com...] [R=301,QSA,L]

Basically removing everything after /m/ and this works now.

g1smd

9:46 pm on Mar 12, 2010 (gmt 0)

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



That's looking good.

mfarah01

5:14 am on Mar 15, 2010 (gmt 0)

10+ Year Member



Thanks, also changed all the redirect rules to rewrite as suggested

g1smd

6:50 am on Mar 15, 2010 (gmt 0)

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



Let's see the final code as there may be other things that can be optimised...

mfarah01

8:15 am on Mar 15, 2010 (gmt 0)

10+ Year Member



RewriteEngine On
RewriteRule ^ntour\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^members\.jsp$ [m.olddomain.com...] [R=301,L]
RewriteRule ^vtourr\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^vtour\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^ptourr\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^ptour\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^freeImg\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^freeVid\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^testfree\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^freejoin\.jsp$ [olddomain.com...] [R=301,L]
RewriteRule ^daily-delivery\.php$ [olddomain.com...] [R=301,L]
RewriteRule ^daily-video\.php$ [olddomain.com...]
RewriteRule ^daily-image\.php$ [olddomain.com...] [R=301,L]
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^(.*)$ [newdomain.com...] [R=301,QSA,L]

g1smd

11:45 am on Mar 15, 2010 (gmt 0)

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



You can combine rules for vtour and vtourr by using
vtourr?
making the final r optional.

You can combine multiple rules like freeVid and testfree by using a local OR construct like
(freeVid|testfree)
here.

Use both together for your actual rule and you can better than halve the number of rules.

The (freeVid|testfree) could also be done as ((test)?free(Vid)?) too, but that's not necessary.

[edited by: g1smd at 11:49 am (utc) on Mar 15, 2010]

g1smd

11:46 am on Mar 15, 2010 (gmt 0)

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



Use example.com to stop the forum busting your code.

mfarah01

12:21 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



Thanks, so would that look like this;

RewriteRule ^(freeVid|testfree)\.jsp$ http://www.example.com/m/daily-video.php [R=301,L]

g1smd

12:34 pm on Mar 15, 2010 (gmt 0)

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



Yes, and you can combine a lot more into that rule.

I kept the examples simple.

mfarah01

1:42 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



OK so I've re-written as follows

RewriteEngine On
RewriteRule ^members\.jsp$ [m.example.com...] [R=301,L]
RewriteRule ^(freeVid|testfree|ntour|vtourr|freejoin|testfree)\.jsp$ http://www.example.com/m/daily-video.php [R=301,L]
RewriteRule ^(freeImg|ptour|ptourr)\.jsp$ http://www.example.com/m/daily-image.php [R=301,L]
RewriteRule ^(daily-video|daily-delivery)\.php$ http://www.example.com/m/daily-video.php [R=301,L]
RewriteRule ^daily-image\.php$ http://www.example.com/m/daily-image.php [R=301,L]
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,QSA,L]

Whilst doing this, i realised that there was another sub folder on the old domain /sms/ that needs to be forwarded to a different domain [m.olddomain.com...]

Should I also use a rewrite here and can you advise on how to do this for a folder?

g1smd

2:29 pm on Mar 15, 2010 (gmt 0)

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



The entry "testfree" appears twice.

You forgot to add a ? to
vtourr
so that it will also match
vtour
as well. Attention to every detail is very important!

The
ptour
and
ptourr
can be combined as
ptourr?
here.

You can further combine
(daily-video|daily-delivery)
into
daily-(video|delivery)
too.

For a folder, you'll need to 'catch' the variable part of the URLs in a backreference and reuse that for the target.

mfarah01

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

10+ Year Member



Cool I think this should be right now. Please can you give me a bit more guidance on 'catching' the back reference?

RewriteEngine On
RewriteRule ^members\.jsp$ [m.example.com...] [R=301,L]
RewriteRule ^(freeVid|testfree|ntour|vtourr?|freejoin)\.jsp$ http://www.example.com/m/daily-video.php [R=301,L]
RewriteRule ^(freeImg|ptourr?)\.jsp$ http://www.example.com/m/daily-image.php [R=301,L]
RewriteRule ^(daily-(video|daily-delivery)\.php$ http://www.example.com/m/daily-video.php [R=301,L]
RewriteRule ^daily-image\.php$ http://www.example.com/m/daily-image.php [R=301,L]
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,QSA,L]

g1smd

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

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



There's one in your ruleset. It's the ( ) and $1 parts.


I wondered if having told you about combining the "daily-" URLs whether you'd spot the same opportunity with freeVid and freejoin too? :)

The "daily-" rule has unmatched parentheses and will match only daily-video and daily-daily-delivery. Details!

mfarah01

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

10+ Year Member



LOL, ok I see what you mean, here is the revised code with my attempt at the variable catch for the folder

RewriteEngine On
RewriteRule ^/sms/(.*) [m.example.com...] [R=301,QSA,L]
RewriteRule ^members\.jsp$ [m.example.com...] [R=301,L]
RewriteRule ^(free-(Vid|join)|testfree|ntour|vtourr?)\.jsp$ http://www.example.com/m/daily-video.php [R=301,L]
RewriteRule ^(freeImg|ptourr?)\.jsp$ http://www.example.com/m/daily-image.php [R=301,L]
RewriteRule ^(daily-(video|daily-delivery))\.php$ http://www.example.com/m/daily-video.php [R=301,L]
RewriteRule ^daily-image\.php$ http://www.example.com/m/daily-image.php [R=301,L]
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,QSA,L]

g1smd

5:13 pm on Mar 15, 2010 (gmt 0)

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



Still matching daily-daily-delivery. :)

mfarah01

7:43 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



I "think" I'm there now :)

RewriteEngine On
RewriteRule ^/sms/(.*) [m.example.com...] [R=301,QSA,L]
RewriteRule ^members\.jsp$ [m.example.com...] [R=301,L]
RewriteRule ^(free(Vid|join)|testfree|ntour|vtourr?)\.jsp$ http://www.example.com/m/daily-video.php [R=301,L]
RewriteRule ^(freeImg|ptourr?)\.jsp$ http://www.example.com/m/daily-image.php [R=301,L]
RewriteRule ^(daily-(video|delivery))\.php$ http://www.example.com/m/daily-video.php [R=301,L]
RewriteRule ^daily-image\.php$ http://www.example.com/m/daily-image.php [R=301,L]
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,QSA,L]

Is my /sms/ rule ok?

jdMorgan

12:33 am on Mar 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remove the leading slash from the "^/sms/(.*)$" pattern.

Jim

mfarah01

7:57 am on Mar 16, 2010 (gmt 0)

10+ Year Member



Thanks, so it would look like this?

RewriteRule ^sms/(.*) [m.example.com...] [R=301,QSA,L]

g1smd

8:03 am on Mar 16, 2010 (gmt 0)

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



With http:// in place of the [ yes.

mfarah01

9:02 am on Mar 16, 2010 (gmt 0)

10+ Year Member



Yes, tested and all working. Thanks very much to both of you for your help. I've learnt a lot!

jdMorgan

3:45 pm on Mar 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You really don't need "[QSA]" on most (or all) of your rules. [QSA] is only needed if you wish to retain the requested query string *and* append one or more new parameters to it. Any other use is simply a waste of CPU time.

Jim