How to force WWW in a URL

I’ve come across this problem today so I’ll leave here the solution if you are using a Apache server.

So Editing (or adding ) the .htaccess you need to have this code included there:

RewriteEngine on

For FORCING www.
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^(YOUR_DOMAIN_NAME\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

For REMOVING www.
RewriteCond %{HTTP_HOST} !^www.YOUR_DOMAIN_NAME.com$
RewriteRule ^(.*)$ http://www.YOUR_DOMAIN_NAME.com/$1 [R=301]

This should solve it.
Advertisements

Leave a comment

Filed under Apache

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s