How to Add a www Resolve to Apache for Better SEO

Written by Zac Vineyard in Code

There is a simple way to resolve your site URLs to include the www in each address. Or drop the www from each address. Why is this important? "Redirecting requests from a non-preferred hostname is important because search engines consider URLs with and without "www" as two different websites" (woorank.com). So, if you don't resolve your www, you are harming your SEO performance.

Here are the three lines of code you can to a .htaccess file to resolve your www to include the www.

Make sure to place the .htaccess file you build in the root directory of your web site.

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


Here are the three lines of code to use if you want to drop the www.

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


Make sure you replace "example.com" with your own domain in the sample code.