.htaccess redirect all pages to new domain
.HtaccessMod Rewrite.Htaccess Problem Overview
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?
.Htaccess Solutions
Solution 1 - .Htaccess
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
Solution 2 - .Htaccess
May be like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
Solution 3 - .Htaccess
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]
Solution 4 - .Htaccess
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>
Solution 5 - .Htaccess
<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
Solution 6 - .Htaccess
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
Solution 7 - .Htaccess
If you want to redirect from some location to subdomain you can use:
Redirect 301 /Old-Location/ http://subdomain.yourdomain.com
Solution 8 - .Htaccess
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]
Solution 9 - .Htaccess
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]
Solution 10 - .Htaccess
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]
Solution 11 - .Htaccess
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();
}
Solution 12 - .Htaccess
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?).
Solution 13 - .Htaccess
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/ [??]
Solution 14 - .Htaccess
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.)
Solution 15 - .Htaccess
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]
Solution 16 - .Htaccess
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.
Solution 17 - .Htaccess
The simplest way is actually just:
Redirect 301 / http://www.newsite.com/
source: https://github.com/phanan/htaccess#redirect-an-entire-site
Solution 18 - .Htaccess
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]
Solution 19 - .Htaccess
This may be work Properly
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</IfModule>