13

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

Amol Ghotankar
  • 2,008
  • 5
  • 28
  • 42
  • Asking google would have been faster than adding a question here. ;-) – Qben Oct 14 '13 at 09:35
  • This will redirect all request of new and old domain to new domain, but I need only old domain urls to be redirected to new domain. Isn't this a different question than what is generally asked in google. – Amol Ghotankar Oct 15 '13 at 03:59
  • When running with `mod_rewrite` I would suggest you enable the `RewriteLog` to see what is actually happening. – Qben Oct 15 '13 at 05:36

3 Answers3

18

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.
Community
  • 1
  • 1
Qben
  • 2,617
  • 2
  • 24
  • 36
  • Problem I feel here is both my new domain and old domain are looked over by same apache is such redirect fair enough? – Amol Ghotankar Oct 14 '13 at 13:39
  • 1
    Sure, performance wise it's better than fire up `mod_rewrite` and rewrite stuff. But I guess it's mostly up to what you feel comfortable to use. The recommendation for these kinds of things is however to use `Redirect` over `RewriteRule`. – Qben Oct 14 '13 at 13:52
2

I also recommend to use an If statement as you can use it also in a multisite server. Just type:

<If "%{HTTP_HOST} == 'old.example.com'">
    Redirect "/" "https://new.example.com/"
</If>
K. Stopa
  • 597
  • 8
  • 9
-3

Write the below code in to your .htaccess and it will redirect all your old domain request to new domain.

RewriteEngine on
RewriteBase /
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
linuxnewbee
  • 998
  • 2
  • 10
  • 23
  • This will redirect all request of new and old domain to new domain, but I need only old domain urls to be redirected to new domain. – Amol Ghotankar Oct 15 '13 at 04:00