Redirecting Domains With htaccess

Yesterday, we explored how to redirect a domain from your domain registrar. Today, I’ll share some code how to do the same thing by editing the htaccess file in your old website. This only works if you have a web host running an Apache server. Don’t sweat over this small technicality. Usually, if you have Linux based web host, chances are this will work.

So here’s the code. It’ll redirect the domain and also retain any page name trailing the domain.

Example http://olddomain.com/somepage.html will be redirected to http://newdomain.com/somepage.html

Make sure you change out the olddomain.com and newdomain.com for your own domain names. 

 

RewriteEngine On

RewriteCond %{HTTP_HOST} ^.*olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

Technorati Tags: , ,

Scroll to Top