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

September 1, 2014 5.6k views

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

3 Answers

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>
  • Thanks Roman, which file would I have to change? apache2.conf?

  • This would be in : /etc/apache2/sites-available/

    You would edit your site file such as "default" or yourdomain.com

    RP

  • Thats where I am confused, I don't have a site file in there and I also don't have "default". I do have 000-default.conf and default-ssl.conf are these correct?

  • I think you're looking inside /etc/apache2/sites-enabled/, check inside /etc/apache2/sites-available/

    However 000-default.conf is just a symlink of default, so editing either will work.

    AFAIK setting the site address and wordpress address to the non-www domain name in WP-Admin > Settings > General redirects your site. So I don't think editing service config files is needed.

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;
}
Have another answer? Share your knowledge.