Redirect all request from old domain to new domain

Amol Ghotankar picture Amol Ghotankar · Oct 14, 2013 · Viewed 37.2k times · Source

I am looking to migrate from old domain to new domain.

I have my old domain olddomain.com and new domain newdomain.com pointing to same ip address for now.

I have Apache server inplace to handle requests.

How do I 301 redirect all my

olddomain.com/*

&

www.olddomain.com/*

to

newdomain.com/*

Can I get exact regex or configuration that I need to add in htaccess.

My newdomain.com and olddomain.com both are being serverd by same apache from same IP so "/" redirect might lead to cycles? And so was looking for effecient way

I tried

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^localhost$ [OR]
    #  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
    RewriteRule (.*)$ http://comp16/$1 [R=301,L]
</IfModule>

And even tried adding in virtual host

RedirectMatch (.*)\.jpg$ http://comp17$1.jpg 

But it does not redirect site when i hit localhost in browser to my computer name i.e comp16

Answer

Qben picture Qben · Oct 14, 2013

In the configuration (VirtualHost) for each of your olddomain.com host try this:

Redirect permanent / http://newdomain.com/

Apache documentation for Redirect. This is the preferred way when everything should be redirected. If you must use mode_rewrite/htaccess there are plenty of questions around this on SO and one of them is:

How do I 301 redirect one domain to the other if the first has a folder path

EDIT
Recommendation from Apache regarding simple redirects:

mod_alias provides the Redirect and RedirectMatch directives, which provide a means to
redirect one URL to another. This kind of simple redirection of one URL, or a class of 
URLs, to somewhere else, should be accomplished using these directives rather than 
RewriteRule. RedirectMatch allows you to include a regular expression in your 
redirection criteria, providing many of the benefits of using RewriteRule.