281

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?

4
  • 10
    To save time, scroll down to the right best answer which is RewriteEngine on RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L] * stackoverflow.com/a/7578810/1066234
    – Avatar
    Oct 20, 2015 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 7, 2017 at 12:28
  • If you are looking for an htaccess tutorial, you can find it on the following link helponnet.com/2021/04/15/htaccess-tutorial-for-beginers
    – Amit Verma
    Jan 9, 2022 at 18:41
  • @Avatar Except that that answer is not answering the question being asked! (And has since been edited anyway.)
    – MrWhite
    Mar 23 at 21:47

20 Answers 20

519

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/ [R=301,L]
www.example.net/somepage.html?var=foo

redirects to:

www.newdomain.com
10
  • 24
    won't this redirect EVERY request though, even those for newdomain.com? And essentially cause two duplicate requests in those cases?
    – Bill Dami
    Aug 2, 2013 at 17:02
  • 39
    The question is to redirect everything to the home page
    – kravits88
    Jan 23, 2014 at 0:23
  • 36
    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, 2014 at 17:28
  • How we can do for redirecting from subdomain to new domain? Jun 17, 2015 at 6: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, 2016 at 2:20
250

May be like this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
8
  • Um, could you try reload your browser? its even redirecting with xampp here. I just tested.
    – YOU
    Dec 22, 2009 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, 2009 at 10:59
  • 32
    Wouldn't it be RewriteCond %{HTTP_HOST} !^NEWDOMAIN\.com$ [NC]?
    – Jim Geurts
    Jun 21, 2012 at 18:28
  • 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, 2017 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, 2017 at 19:00
97

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]
4
  • 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. Aug 8, 2016 at 14:50
  • This should be the correct answer! Redirect everything from old domain to the new domain period. Works like a charm! And yes hosting redirect must be removed. The best is hash entire .htaccess living only this rule at the top. Thanks
    – Maryando
    Dec 21, 2022 at 11:00
  • You should also include the L flag. If you have any other directives (not that you necessarily would in this particular case) then this will likely fail. And this does assume the old and new domains point to different servers.
    – MrWhite
    Mar 23 at 11:57
  • this worked where others didn't for me in 2023
    – mdkb
    May 15 at 2:14
43
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
  RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

This worked for me

4
  • Yes, this method would be clean and nice !, worked for me as well.
    – Cody
    Aug 30, 2018 at 7:57
  • 1
    this will redirect with the parameter in URl if any - index.php?aa=9 will carry the parameter to new domain May 15, 2019 at 15:55
  • 3
    The safest solution. Especially when both domains are controlled by the same .htaccess. Aug 23, 2021 at 8:21
  • 3
    In my opinion, this answer should be the chosen answer since it covers the most common redirect cases. The only thing I would add is that you can change [OR] to [NC,OR] if you also need case-insensitivity. See the RewriteRule Flags for more info.
    – Tyler
    Dec 31, 2021 at 20:30
28

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>
3
  • 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 5, 2015 at 21:54
  • 1
    The key here is that the domain rewrite rule comes before the WordPress index rewrite rule.
    – MrMoxy
    Jun 15, 2016 at 4:36
  • This answer is very good. It can be redirect anything that is not in newdomain.com to newdomain.com including olddomain.com, sub1.olddomain.com to sub999.olddomain.com in just one condition.
    – vee
    Mar 22, 2022 at 5:12
18

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
5
  • 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 May 23, 2016 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 7, 2017 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 7, 2017 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 5, 2017 at 19:20
  • But the question related to .htaccess so why mention the vhost file? Jul 5, 2017 at 19:39
12

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

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

4
  • It's the best way. Because much Webhosters didn't allow use of RewriteEngine in .htaccess. This works great anyway! Jan 10, 2015 at 0: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, 2015 at 5: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. Jan 13, 2015 at 21:24
  • 1
    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 9, 2021 at 18:23
8

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]
1
  • 1
    that actually worked for me , need a solution with out passing any slug to new domain Thanks Dsynmphony May 8, 2020 at 6:00
7

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] 
7

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]
1
  • The "highest rated answer" was actually corrected a month after this answer was posted.
    – MrWhite
    Feb 9, 2021 at 18:27
5

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?).

0
4

To swap out the domain and leave the requested path and query as is:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,QSA,L]
</IfModule>

%{REQUEST_URI} is an alias for the requested path.

R=301 is a HTTP 301 Moved Permanently

L means last rule, do not consider any more conditions.

QSA meaning Query String Appended keeps any query string (e.g. ?foo=bar)

To adjust the domain and requested path then do this:

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

^(.*)$ captures everything in the request path from start to the end.

$1 outputs the first captured group

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();
}
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/ [??]
2
  • I banged my head against this for nearly a month Dec 22, 2009 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 9, 2021 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.)

1
  • 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, 2009 at 11:46
2

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]
2

The simplest way is actually just:

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

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

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.

1

This may be work Properly

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</IfModule>
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]

Not the answer you're looking for? Browse other questions tagged or ask your own question.