Forcing or eliminating the WWW

Web programming topics
Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

Forcing or eliminating the WWW

Post by Tony » Sat Nov 28, 2009 6:24 pm

If you want your website to always use the WWW. prefix (like https://robot.lk) then this htaccess code is your answer. You may want to force the WWW because you like it that way, or because you want consistency for search engines. What ever the reason, use this htaccess code and you will always have the WWW domain prefix used for your website.

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
The way this works is if the domain name does not start with WWW then the request is rewritten with the WWW. The WWW is really just treated as any other subdomain. So if you have other subdomains (like sub.robot.lk) where you do not want the WWW added (converted to http://www.sub.robot.lk), then you need to include another RewriteCond line for each one.

Or if you want to always eliminate the www prefix, use this htaccess code.

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Post Reply

Return to “Web programming”