Question

Correct way to redirect www to non www Ubuntu 14.04 VPS?

Hi,

I have searched on here for the answer for my questions, but seem to get varied responses.

I would like to know the best practice to redirect the www version of my site to the non www version.

I have already created the CNAME record as below:

CNAME www @

This works fine, but I get a redirected warning in Google Webmaster Tools where on my previous host it worked fine. (Maybe this isn’t an issue?)

I have checked my old .htaccess of my previous host (SiteGround) and it was not being redirected from there.(They also had the CNAME record for www) So this has lead me to believe that will need to be created as a virtual host in apache as well?

In /etc/apache2/ there is apache2.conf which seems to be my current settings as /etc/apache2/sites-available only includes the files 000-default.conf and default-ssl.conf which both have no virtual hosts referencing my server.

So my question is what do I have to do next, if anything?

Many thanks

Tyler


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Most servers use Nginx now. For example Serverpilot on Digitalocean. So the correct way would be to use Nginx redirect.

create file with

if ($host ~* www.appname.com$) {
    return 301 http://appname.com$request_uri;
}

ant put in

/etc/nginx-sp/vhosts.d/appname.d/0www_redirect.conf

taken from http://techcat.ca/threads/nginx-force-www-to-no-www-domain-redirect-on-serverpilot-digitalocean.17/

I did this as the only change to my serverpilot config (so I left the main.conf as it was), but I get a too many redirects error. Any ideas? This is saweb.conf in nginx-sp/hosts.d:

server {
    listen       80;
    listen       [::]:80;
    server_name
        domainnamereplaced.com
        www.domainnamereplaced.com
      ;

    root   /srv/users/serverpilot/apps/saweb/public;

    access_log  /srv/users/serverpilot/log/saweb/saweb_nginx.access.log  main;
    error_log  /srv/users/serverpilot/log/saweb/saweb_nginx.error.log;

    proxy_set_header    Host              $host;
    proxy_set_header    X-Real-IP         $remote_addr;
    proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;

    include /etc/nginx-sp/vhosts.d/saweb.d/*.nonssl_conf;
    include /etc/nginx-sp/vhosts.d/saweb.d/*.conf;
}

Tyler - Check out the following information for Apache:

http://httpd.apache.org/docs/current/rewrite/remapping.html

Description:
The goal of this rule is to force the use of a particular hostname, in preference to other hostnames which may be used to reach the same site. For example, if you wish to force the use of www.example.com instead of example.com, you might use a variant of the following recipe.
Solution:
The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).

<VirtualHost *:80>
  ServerName undesired.example.com
  ServerAlias example.com notthis.example.com

  Redirect / http://www.example.com/
</VirtualHost>

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