How can I force visitors to my website to the www or non-www URL?

cPanel cPanel Write a comment

When you host your website with us, visitors can reach your website using either the www or non-www form of your domain name.

Sometimes you may find a need for having visitors to your website forced to use one or the other. One such case is when you have an SSL certificate installed to enable secure https URLs. An SSL certificate is only valid for a single hostname, so you want your visitors to be using that hostname before they hit the secure area of your site in order to ensure they don't get a security warning.

Here are the directives you should add to the .htaccess file in your public_html directory to force visitors to use the www form of your domain:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.domain\.com)?$
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule (.*) http%2://www.domain.com/$1 [R=301,L]

Here are the directives you should add to the .htaccess file to force visitors to use the non-www form of your domain:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.domain\.com)?$
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule (.*) http%2://domain.com/$1 [R=301,L]

IMPORTANT: Replace domain.com with your domain name.

Write comments...
or post as a guest
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.