00 Votes

HTACCESS: Redirect requests without www to www - example.com to www.example.com

Question by Compi | 2017-10-25 at 16:57

At the moment, my website is accessible via two different ways. A request for http://example.com as well as for http://www.example.com are both leading to my site - and that for each single page!

Therefore, my website is effectively reachable via two different addresses or domains. Gladly, I would like to redirect all calls to my domain without www to the domain with www. So, for example, the request for http://example.com should be redirected to http://www.example.com.

Can someone explain how to do that? I think the best way would be using and .htaccess file.

ReplyPositiveNegative
2Best Answer2 Votes

Just use the following Redirect in your .htaccess file (a file with the name .htaccess in the root folder of your website):

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R=301,L] 

Of course, you have to change example.com to the name of your domain in order to use this example.

The Rewrite-Condition matches all cases not starting with "www.example.com" and then they will be redirected to http://www.example.com. With %{REQUEST_URI} you make sure that you do not lose any parts of the further URL. The HTTP-Status-Code 301 (Moved Permanently) tells every requester that your page can from now on only be found under the new address.
2017-10-25 at 18:38

ReplyPositive Negative
Reply

Related Topics

HTACCESS: Simplify URL

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.