.htaccess – Redirect all URLs to a specific sub domain

Sometimes you may have multiple domains which you want to use for your website or web application. Probably you needed to park the domains on your hosting cpanel. In this case, you website or web application could be access by more than one domain which is not good for SEO and other 3rd party services may not work.

A simple way to redirect all URLs to a specific URLs is by .htaccess.

Suppose you have the following sub domains and all of them serve your website or web application.


To redirect all visitors to a specific sub domain say http://www.ykyuen.com.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^ykyuen.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^ykyuen.com.hk$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^www.ykyuen.com.hk$ [NC]
  RewriteRule ^(.*)$ http://www.ykyuen.com/$1 [L,R=301]
</IfModule>

 

Or you can use the NOT character (!)

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www.ykyuen.com$ [NC]
  RewriteRule ^(.*)$ http://www.ykyuen.com/$1 [L,R=301]
</IfModule>

 

Done =)

8 thoughts on “.htaccess – Redirect all URLs to a specific sub domain”

    1. haha~ i need to work on the .htaccess becoz the client is using a shared hosting. so i couldn’t touch anything on the webserver except by the .htaccess~

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.