Amol Ghotankar
Amol Ghotankar

Reputation: 2094

Redirect all request from old domain to new domain

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

Upvotes: 13

Views: 46016

Answers (4)

Aris Boy
Aris Boy

Reputation: 41

This one should do it:

Create file .htaccess inside root web directory.

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.*)?$  http://newcomain.com/$1 [R=301,L]
</IfModule>

It will redirect all requests with parameters to the new server

Upvotes: 0

K. Stopa
K. Stopa

Reputation: 767

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>

Upvotes: 2

Qben
Qben

Reputation: 2623

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.

Upvotes: 18

linuxnewbee
linuxnewbee

Reputation: 1008

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]

Upvotes: -3

Related Questions