Normally your website is accessible both with and without ‘www.’ prefix. And if you want your users to redirect to ‘www.’ prefix when they access your website without ‘www.’ prefix. You can do that using ‘.htaccess’ file.
We use .htaccess file to make configuration changes on per-directory basic. You need to place this .htaccess file in your server’s root directory.
Basically, we need to write some rewrite rules for our URL in this file.
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} ^yourwebsite.com$ [NC] RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301] </IfModule>
Reverse of this is also possible. If we want to redirect all website users to access the website without the ‘www.’ prefix. Then we need to write the following rewrite rules in out .htaccess file.
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} ^www.yourwebsite.com$ [NC] RewriteRule ^(.*)$ http://yourwebsite.com/$1 [L,R=301] </IfModule>
Note:- The “mod_rewrite” module should be enabled in Apache httpd.conf file. Otherwise URL rewriting will not work. For enabling this module you need to remove ‘#’ before
LoadModule rewrite_module modules/mod_rewrite.so
line in your httpd.conf file. We are also checking in our .htaccess file that the mod_rewrite module is enabled or not. You should also restart your Apache server to apply these changes.
#1 by sohail - December 28th, 2009 at 04:36
what is the advantage of accessing the website with www. ?