how to removeredirect 301 or make the www as the main and non-www one with redirect

November 10, 2014 572 views

i have created my droplet with the hostname website.com(example) and then add a CNAME with www that points my site. now my site with www shows one 301 redirect.
how can i change this state. i want to remove that 301 redirect from www and want to add that redirect to website.com so that anyone who type website.com lands in www.website.com and www.website.com stays without any redirect.
im on ubuntu 14.04 with apache


1 Answer

Assuming you are using Apache, you can set up a redirect from the non-www url to the www url by setting up a new VirtualHost that just redirects to the other one. It would look something like:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/

<VirtualHost *:80>
    ServerName www.example.com
    # All your normal configuration
Have another answer? Share your knowledge.