Use a 301 redirect . htaccess to 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.
What Is a 301 Redirect? A 301 redirect is a permanent redirect. When a user tries to access an old URL, the server sends their browser the 301-Permanently Moved status code and sends them to another page. This is useful for site owners and users because it means they are directed to the next most relevant page.
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
May be like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
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]
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With