Forum Moderators: phranque

Message Too Old, No Replies

Redirect URL, whole site, mod rewrite

How to redirect all requests for old URL to new URL

         

Martin_Sach

11:10 am on May 5, 2005 (gmt 0)

10+ Year Member



I am trying to find a way of using mod rewrite in the htaccess file to ensure that when users click on a search engine link to an old but still working URL www.oldurl.co.uk/anyfile.htm they are redirected to www.newurl.co.uk/samefile.htm. I can do this for a single html file that has been moved or renamed. However I'm struggling to find an online guide to doing this for the whole domain name. I want both domain names to continue to point at the same webspace but I want to ensure that nobody ever sees oldurl.co.uk in the browser address bar ever again. I'm on a Zeus server though this might change to Apache later.
Can anyone advise or point me to a tutorial?

jonrichd

10:33 pm on May 5, 2005 (gmt 0)

10+ Year Member



Martin,

If the two domains are actually pointing to the same filespace, then you can use .htaccess to look at the HTTP_HOST parameter, and redirect to the main domain if that's not what was asked for, e.g.


RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.desireddomain\.com
RewriteRule ^(.*)$ htt p://www.desireddomain.com/$1 [R=permanent,L]

That code says: If the HTTP host isn't the desired domain, then do a permanent redirect to the desired domain, and include any file specs that were in the request.

If, on the other hand the file names have changed from the old domain to the new domain, you have a bigger problem.

You'll have to go in for each file that has changed and use a line like this:

RedirectPermanent oldfile.htm htt p://www.newdomain.com/newname.htm

(in both cases, take out the space between the htt and the p).

It takes some time to get everything together, but that's the only way I know to do it.