239

Which redirect rule would I use to redirect all pages under olddomain.example to be redirected to newdomain.example?

The site has a totally different structure, so I want every page under the old domain to be redirected to the new domain index page.

I thought this would do (under olddomain.example base directory):

RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.example/ [R=301]

But if I navigate to olddomain.example/somepage I get redirected to newdomain.example/somepage. I am expecting a redirect only to newdomain.example without the page suffix.

How do I keep the last part out?

Yuval Adam
  • 149,388
  • 85
  • 287
  • 384
  • 9
    To save time, scroll down to the right best answer which is `RewriteEngine on` `RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]` * http://stackoverflow.com/a/7578810/1066234 – Avatar Oct 20 '15 at 11:10
  • 2
    It's worth pointing out that, particularly from an SEO perspective, having a many-to-one redirect to the index/home page is generally a bad idea. Google is likely to see it as a soft-404. – MrWhite Jan 07 '17 at 12:28

19 Answers19

469

The below answer could potentially cause an infinite redirect loop...

Here, this one redirects everything after the domain name on the URL to the exact same copy on the new domain URL:

RewriteEngine on 
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
www.example.net/somepage.html?var=foo

redirects to:

www.newdomain.com/somepage.html?var=foo
Mostafa Norzade
  • 1,059
  • 2
  • 17
  • 35
  • 23
    won't this redirect EVERY request though, even those for newdomain.com? And essentially cause two duplicate requests in those cases? – Bill Dami Aug 02 '13 at 17:02
  • 34
    The question is to redirect everything to the home page – kravits88 Jan 23 '14 at 00:23
  • 32
    Though this is not the answer of the question, but I came to this question from SE seeking exactly this answer. thanks !!! – dav Mar 16 '14 at 17:28
  • How we can do for redirecting from subdomain to new domain? – Sukhjinder Singh Jun 17 '15 at 06:05
  • 3
    Could someone answer Bill's question please. Shouldn't this use a RewriteCond to prevent a duplicate request and/or unnecessary re-writes? I suppose it wouldn't have any noticeable impact to re-write every time even though it isn't necessary. But why do it. – Andrew Mar 29 '16 at 02:20
  • you are god man. however, you forgot to put a slash before &1. it should be RewriteEngine on RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L] – funky-nd Nov 08 '17 at 14:08
232

May be like this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
carla
  • 1,728
  • 1
  • 30
  • 35
YOU
  • 106,832
  • 29
  • 175
  • 207
  • Um, could you try reload your browser? its even redirecting with xampp here. I just tested. – YOU Dec 22 '09 at 10:58
  • My bad... it does work, my hosting service had some other redirect on which was fu**** things up. Thanks! :) – Yuval Adam Dec 22 '09 at 10:59
  • 31
    Wouldn't it be `RewriteCond %{HTTP_HOST} !^NEWDOMAIN\.com$ [NC]`? – Jim Geurts Jun 21 '12 at 18:28
  • Didn't work for me. The code from Yuval Adam (see below) works like a charm. – xaviert Sep 08 '12 at 09:45
  • Fwiw here's a good explanation of the syntax for a solution to a very similar problem: [Migrate domains](http://enarion.net/web/htaccess/migrate-domains/). – JohnK Oct 21 '12 at 13:24
  • may be I should remove RewriteCond, but I never had a chance to test it. If someone know better, may be go ahead and edit it. – YOU Jan 27 '16 at 14:56
  • 1
    After a long winter I edited the code so it can finally work. (the correct is to redirect if EQUAL and not DIFFERENT to olddomain, that ! was the problem) – carla Jan 20 '17 at 18:59
  • 6
    @JimGeurts Only if you want to redirect EVERYTHING that is not newdomain. However, in this case, we wanted to redirect ONLY olddomain. – carla Jan 20 '17 at 19:00
87

Just to clarify, after removing the hosting redirect which was in the way, my original solution also works:

RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]
Yuval Adam
  • 149,388
  • 85
  • 287
  • 384
  • 1
    This will redirect to the default index, which is what your question was about. Just for clarity, since, due to proximity, your statement appears to be directed toward user968421's answer just above this, user968421's answer is correct when the intent is to redirect to a corresponding file name on the new domain. – RationalRabbit Aug 08 '16 at 14:50
24

I've used for my Wordpress blog this as .htaccess. It converts http://www.blah.example/asad, http://blah.example/asad, http://www.blah.example2/asad etc, to http://blah.example/asad Thanks to all other answers I figured this out.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^YOURDOMAIN\.example$ [NC]
RewriteRule ^(.*)$ https://YOURDOMAIN.example/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Daantje
  • 1,945
  • 20
  • 19
  • 2
    I consider this the most comprehensive answer of all: it preserves ranking for every page and is a catchall for all domains that are not the primary one! What could be better? – nzn Aug 05 '15 at 21:54
  • 1
    The key here is that the domain rewrite rule comes _before_ the WordPress index rewrite rule. – MrMoxy Jun 15 '16 at 04:36
  • 1
    @MrWhite Thx, I've corrected my answer. – Daantje Feb 10 '21 at 11:42
20
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
  RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

This worked for me

Iana Sergieieva
  • 353
  • 2
  • 10
14

There are various ways to do this and various redirects, I've listed them below:

301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain:

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://example.com/

302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:

# This allows you to redirect your entire website to any other domain
Redirect 302 / http://example.com/

Redirect index.html to a specific subfolder:

# This allows you to redirect index.html to a specific subfolder
Redirect /index.html http://example.com/newdirectory/

Redirect an old file to a new file path:

# Redirect old file path to new file path
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html

Redirect to a specific index page:

# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html
RustyIngles
  • 2,268
  • 4
  • 23
  • 28
  • If you add $1 to the first example , like this `Redirect 301 / http://example.com/$1` then all the pages of the original site will be redirected to the new site root – Yassine Mokni May 23 '16 at 14:17
  • @YassinMK The `$1` is not required here, you are thinking of the `RedirectMatch` directive (but then you would still need to define a capturing group for the `$1` to reference). – MrWhite Jan 07 '17 at 14:22
  • Although all these `Redirect` directives will redirect like for like, whereas the OP is wanting to redirect just to the home page of the new site (although that in itself is not to be recommended). You might need to use the `RedirectMatch` directive if you wanted to just redirect to a specific URL. – MrWhite Jan 07 '17 at 14:23
  • instead of 301 you could also write Redirect permanent / h ttps://www.example.com, you can do this in the vhost file – Yvon Huynh Jul 05 '17 at 19:20
  • But the question related to .htaccess so why mention the vhost file? – RustyIngles Jul 05 '17 at 19:39
9

If you want to redirect from some location to subdomain you can use:

Redirect 301 /Old-Location/ http://subdomain.yourdomain.com

EmptySD
  • 101
  • 1
  • 6
  • It's the best way. Because much Webhosters didn't allow use of RewriteEngine in .htaccess. This works great anyway! – Sprinterfreak Jan 10 '15 at 00:14
  • You must use RewriteEngine to redirect. Reason why you had problem is that RewriteEngine was already active and you needed only redirect rule. I'm glad that helped. – EmptySD Jan 11 '15 at 05:31
  • Not true. It skips all Rewrite thing and when using `RewriteEngine On` throws 500 Internal Error. It's a very restricted Webspace. Using `Redirect 301 / http://foo` is the only possible way to deal with. – Sprinterfreak Jan 13 '15 at 21:24
  • This will result in a malformed redirect since you are missing a trailing slash on the target URL. With the `Redirect` directive you need to either include or omit the trailing slash on _both_ the source and target URLs. – MrWhite Feb 09 '21 at 18:23
6

If the new domain you are redirecting your old site to is on a diffrent host, you can simply use a Redirect

Redirect 301 / http://newdomain.com

This will redirect all requests from olddomain to the newdomain .

Redirect directive will not work or may cause a Redirect loop if your newdomain and olddomain both are on same host, in that case you'll need to use mod-rewrite to redirect based on the requested host header.

RewriteEngine on


RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^ http://newdomain.com%{REQUEST_URI} [NE,L,R] 
Amit Verma
  • 38,175
  • 19
  • 80
  • 104
5

My reputation won't allow me to comment on an answer, but I just wanted to point out that the highest rated answer here has an error:

RewriteEngine on 
RewriteRule ^(.*)$ http://www.newdomain.com$1 [R=301,L]

should have a slash before the $1, so

RewriteEngine on 
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
3

My solution as SymLinks did not work on my server so I used an If in my PHP.

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}
$redirect = str_replace("www.", "", curPageURL());
$remove_http_root    = str_replace('http://', '', $redirect);
$split_url_array     = explode('/', $remove_http_root );


if($split_url_array[0] == "olddomain.com"){
    header("Location: http://www.newdomain.com/$split_url_array[1]");
    die();
}
Brent
  • 2,125
  • 9
  • 31
  • 60
3

I tried user968421's answer and the OP's solution but the browser popped up a security error for a checkout page. I can't tell you why exactly.

Our host (Site Ground) couldn't figure it out either.

The final solution was close, but a slight tweak to user968421's answer (side note: unlike the OP, I was trying to redirect to the corresponding page, not just to the homepage so I maintained the back reference [the $1 after the domain] from user968421's answer):

RewriteEngine on
RewriteRule (.*) https://newdomain.com/$1 [R=301,L]

Got the tweak from this htaccess redirect generator recommended by a Host Gator article (desperate times, desperate measures, amiright?).

J May
  • 161
  • 3
  • 7
3

Simple just like this and this will not carry the trailing query from URL to new domain.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule .* https://www.newdomain.com/? [R=301,L]
Darksymphony
  • 843
  • 5
  • 22
2

This is a bug in older versions of apache (and thus mod_rewrite) where the path prefix was appended to the rewritten path if it got changed. See here

I think it was fixed in apache2 V2.2.12, there is a special flag you need to use which i will add here when i find it, (i think it was NP for No Path)

RewriteRule ^(.*)$ http://newdomain.com/ [??]
Question Mark
  • 3,546
  • 1
  • 23
  • 30
  • I banged my head against this for nearly a month – Question Mark Dec 22 '09 at 11:12
  • The flag you are probably referring to is `DPI` (Discard Path-Info). However, this should be irrelevant in the context of the question. This will only apply if you have other internal rewrites that contain path-info. However, this flag should be applied to the rewrites, not the final redirect. – MrWhite Feb 09 '21 at 18:51
2

From the usability point of view it would be better, if you also send the path with the request (i.e., what you have at the moment) and let your new site deal with it:

You searched for "/products".

Unfortunately this page is gone. Would you like to visit "/new_products" instead?

(and better, still, doing this automatically.)

This is obviously a lot of coding and heuristics for a larger website, but in my opinion it would pay off in terms of user satisfaction (when your carefully saved bookmark of your dream product just leads you to the front page of newdomain.com, this is frustrating.)

Boldewyn
  • 75,918
  • 43
  • 139
  • 205
  • For said website this is pretty much irrelevant as the whole website has changed, but you are absolutely correct. The website should be as backwards-compatible as possible. +1 – Yuval Adam Dec 22 '09 at 11:46
1

Use conditional redirects with Options -FollowSymLinks and AllowOverride -Options disabled by the Hoster if a few local files should be served too:

Sample .htaccess

# Redirect everything except index.html to http://foo
<FilesMatch "(?<!index\.html)$">
    Redirect 301 / http://foo/
</FilesMatch>

This example will serve local index.html and redirects all other staff to new domain.

Sprinterfreak
  • 484
  • 4
  • 10
1

Try this methode to redirect all to homepage new domain, Its works for me:

RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^(.*)$ "https\:\/\/newdomain\.com\/" [R=301,L]
0

The previous answers did not work for me.

I used this code. If you are using OSX make sure to use the correct format.

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?OLDDOMAIN\.com$ [NC]
RewriteRule (.*) http://www.NEWDOMAIN.com/ [R=301,L]
Alfrex92
  • 4,199
  • 4
  • 21
  • 37
0

The simplest way is actually just:

Redirect 301 / http://www.newsite.com/

source: https://github.com/phanan/htaccess#redirect-an-entire-site

SerialEnabler
  • 418
  • 3
  • 8
0

This may be work Properly

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</IfModule>
GOST
  • 17
  • 6