Which redirect rule would I use to redirect all pages under olddomain.example
to be redirected to newdomain.example
?
The site has a totally
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>
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]
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.
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?).
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();
}
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]