简体   繁体   中英

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 ie comp16

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.

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>

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM