Sometimes we want to switch from one domain to another however we want to make sure that the old links are redirected properly and are still accessible.

This snippet redirects the old links to a new domain.

For example

http://olddomain.com => http://newdomain.com
http://olddomain.com/articles => http://newdomain.com/articles

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteEngine On
    RewriteCond %{HTTP_HOST} yourOLDdomain
    RewriteRule (.*) http://yournewdomain.com%{REQUEST_URI} [L,R=301]
</IfModule>

if the file doesn't exist create it in your www, public_html or htdocs folder.

 

If you want to redirect all the traffic from the old site to the next site use this code.
You can make and exception and allow skipped-folder to be access e.g.  if you have to use an existing software from the old site.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !/skipped-folder
RewriteRule (.*) http://somenewlocation.com/ [L]
</IfModule>

 

If you don't have to make an exception for a folder from the old site just use this

Redirect / http://somenewlocation.com/

 

Note: it is too technical so don't worry if you didn't fully understand those rewrite rules

Do you need any of the code in this post customized or packaged into a plugin? Get a free quote
Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer