Force Use of www in All URLs And Redirect Traffic

How to Force Use of www in All URLs And Redirect Traffic

in Optimization & Best Practice
Google+ LinkedIn

A website can be accessed with http://www.domain.com and http://domain.com. If you visit any of these URLs, you will be sent to the same page but they will have different URLs.

The main problem is that Google and other search engines consider it as duplicate content and therefore penalize this domain in the search engine ranking.

The best way to get around this problem is to make sure that all your URLs on your site use a consistent URL format either www.domain.com or domain.com.

Therefore with the help of htaccess, we can redirect all non-www URL traffic to www.domain.com. You can just copy and paste this snippet below and replace domain.com with your domain.

# Force adding www in domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

Share the love

If you like this snippet, share it with others!

Leave a Reply

Your email address will not be published. Required fields are marked *