tags:

views:

136

answers:

4

Which redirect rule would I use to redirect all pages under olddomain.com to be redirected to newdomain.com?

The site has a totally different structure, so I want every page under the old domain to be redirected to the new domain index page.

I thought this would do (under olddomain.com base directory):

RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]

But if I navigate to olddomain.com/somepage I get redirected to newdomain.com/somepage. I am expecting a redirect only to newdomain.com without the page suffix.

How do I keep the last part out?

+1  A: 

May be like this, not tested

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
S.Mark
nope, still getting a redirect to the pages...
Yuval A
Ah I see, removed $1 part
S.Mark
even after removing the $1 :) still no good...
Yuval A
Really! I really need to test it then.
S.Mark
Um, could you try reload your browser? its even redirecting with xampp here. I just tested.
S.Mark
My bad... it does work, my hosting service had some other redirect on which was fu**** things up. Thanks! :)
Yuval A
Ah, I see. You're welcome.
S.Mark
A: 

Just to clarify, after removing the hosting redirect which was in the way, my original solution also works:

RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]
Yuval A
+1  A: 

This is a bug in older versions of apache (and thus mod_rewrite) where the path prefix was appended to the rewritten path if it got changed. See here

I think it was fixed in apache2 V2.2.12, there is a special flag you need to use which i will add here when i find it, (i think it was NP for No Path)

RewriteRule ^(.*)$ http://newdomain.com/ [??]
Question Mark
I banged my head against this for nearly a month
Question Mark
+1  A: 

From the usability point of view it would be better, if you also send the path with the request (i.e., what you have at the moment) and let your new site deal with it:

You searched for "/products".

Unfortunately this page is gone. Would you like to visit "/new_products" instead?

(and better, still, doing this automatically.)

This is obviously a lot of coding and heuristics for a larger website, but in my opinion it would pay off in terms of user satisfaction (when your carefully saved bookmark of your dream product just leads you to the front page of newdomain.com, this is frustrating.)

Boldewyn
For said website this is pretty much irrelevant as the whole website has changed, but you are absolutely correct. The website should be as backwards-compatible as possible. +1
Yuval A