.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 =)

Advertisements

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s