Wordpress Snippets

Force Use of www in All URLs And Redirect Traffic With .htaccess

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]

Hire Us. Or just say Hi!
Need a job? Apply to get one.