Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

How to redirect www to non-www and non-www to www URL using .htaccess redirect(mod_rewrite)

Websites can be access with www and without www also. Since Search engines consider http://www.programmingfacts.com and http://programmingfacts.com different websites, it might be a case of duplicate content. So you must stick your domain name accessible either with www or without www.

This can be done by writing only two simple lines in your .htaccess file(mod_rewrite in apache). Before writing this rules in .htaccess , make sure that your mod_rewrite is enabled(On). Most probably mod_rewrite is enabled in Linux server but for windows server you need to contact hosting people to make mod_rewrite enabled. You can check this by looking in phpinfo().

Below is the Rules of .htaccess redirect for all www requests to non-www URL.

RewriteCond %{HTTP_HOST} ^www.programmingfacts.com$ [NC]
RewriteRule ^(.*)$ http://programmingfacts.com/$1 [R=301,L]

Redirect 301 means Moved Permanently so most search engines will send visitors directly to the www version of your URL.

Below is the Rules .htaccess redirect for non-www requests to www URL.

RewriteCond %{HTTP_HOST} ^programmingfacts\.com [nc]
RewriteRule (.*) http://www.programmingfacts.com/$1 [R=301,L]

Redirect 301 means Moved Permanently so most search engines will send visitors directly to the non-www version of your URL.This is all about www to non-www .htaccess URL redirect and non-www to www .htaccess URL redirect.

What you need to do is just replace programmingfacts with your domain name when you write www to non-www .htaccess redirection rules and non-www to www .htaccess redirection rules. Look at below links for more .htaccess rules.

To know more about programming,MYSQL database,php info,php editor,programming php,Open-source,php help and php script , subscribe to our feed by entering email address below. You will get updates via email about every tutorial posted on this site . It will not take more than a sec.

Enter your email address:
Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.


Leave a Reply

Your email address will not be published.