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 =)
Don’t use Nginx now?
LikeLike
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~
LikeLike
Btw, I guess can save some Apache power by doing this in DNS to redirect * to .ykyuen.com.hk =)
LikeLike
yes~ actually the redirection can be done either on the .htaccess or by the virtualhost config~
LikeLike
Be careful the file permission of .htaccess. It’s a common weak point to be hacked
LikeLike
haha~ i couldn’t control that neither on a shared hosting~ XD
LikeLike
This is a very nice post on htaccess redirect . Your article is great, i like it
http://www.htaccessredirect301.com
LikeLike
Thanks for your comment. =)
LikeLike