Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx no-www to www and www to no-www

I am using nginx on Rackspace cloud following a tutorial and having searched the net and so far can't get this sorted.

I want www.mysite.com to go to mysite.com as normal in .htaccess for SEO and other reasons.

My /etc/nginx/sites-available/www.example.com.vhost config:

server {        listen 80;        server_name www.example.com example.com;        root /var/www/www.example.com/web;         if ($http_host != "www.example.com") {                  rewrite ^ http://example.com$request_uri permanent;        } 

I have also tried

server {        listen 80;        server_name example.com;        root /var/www/www.example.com/web;         if ($http_host != "www.example.com") {                  rewrite ^ http://example.com$request_uri permanent;        } 

I also tried. Both the second attempts give redirect loop errors.

if ($host = 'www.example.com' ) { rewrite ^ http://example.com$uri permanent; } 

My DNS is setup as standard:

site.com 192.192.6.8 A type at 300 seconds www.site.com 192.192.6.8 A type at 300 seconds 

(example IPs and folders have been used for examples and to help people in future). I use Ubuntu 11.

like image 737
TheBlackBenzKid Avatar asked Oct 30 '11 18:10

TheBlackBenzKid


People also ask

How do I redirect www Nginx to https?

Nginx Redirect all HTTP traffic to HTTPS Here is a breakdown of the commands: Listen 80 : This instructs the system to catch all HTTP traffic on Port 80. Server_name _; : This will match any hostname. Return 301 : This tells the browser (and search engines) that this is a permanent redirect.

Do I need to redirect to non-www?

For most users, it is not of particular importance whether they access a website via a www or a non-www link. However, the fact that a website is accessible with or without the input of “www” can seriously impact a website's search engine ranking.


2 Answers

HTTP Solution

From the documentation, "the right way is to define a separate server for example.org":

server {     listen       80;     server_name  example.com;     return       301 http://www.example.com$request_uri; }  server {     listen       80;     server_name  www.example.com;     ... } 

HTTPS Solution

For those who want a solution including https://...

server {         listen 80;         server_name www.domain.com;         # $scheme will get the http protocol         # and 301 is best practice for tablet, phone, desktop and seo         return 301 $scheme://domain.com$request_uri; }  server {         listen 80;         server_name domain.com;         # here goes the rest of your config file         # example          location / {              rewrite ^/cp/login?$ /cp/login.php last;             # etc etc...          } } 

Note: I have not originally included https:// in my solution since we use loadbalancers and our https:// server is a high-traffic SSL payment server: we do not mix https:// and http://.


To check the nginx version, use nginx -v.

Strip www from url with nginx redirect

server {     server_name  www.domain.com;     rewrite ^(.*) http://domain.com$1 permanent; }  server {     server_name  domain.com;     #The rest of your configuration goes here# } 

So you need to have TWO server codes.

Add the www to the url with nginx redirect

If what you need is the opposite, to redirect from domain.com to www.domain.com, you can use this:

server {     server_name  domain.com;     rewrite ^(.*) http://www.domain.com$1 permanent; }  server {     server_name  www.domain.com;     #The rest of your configuration goes here# } 

As you can imagine, this is just the opposite and works the same way the first example. This way, you don't get SEO marks down, as it is complete perm redirect and move. The no WWW is forced and the directory shown!

Some of my code shown below for a better view:

server {     server_name  www.google.com;     rewrite ^(.*) http://google.com$1 permanent; } server {        listen 80;        server_name google.com;        index index.php index.html;        ####        # now pull the site from one directory #        root /var/www/www.google.com/web;        # done #        location = /favicon.ico {                 log_not_found off;                 access_log off;        } } 
like image 54
TheBlackBenzKid Avatar answered Sep 27 '22 20:09

TheBlackBenzKid


Actually you don't even need a rewrite.

server {     #listen 80 is default     server_name www.example.com;     return 301 $scheme://example.com$request_uri; }  server {     #listen 80 is default     server_name example.com;     ## here goes the rest of your conf... } 

As my answer is getting more and more up votes but the above as well. You should never use a rewrite in this context. Why? Because nginx has to process and start a search. If you use return (which should be available in any nginx version) it directly stops execution. This is preferred in any context.

Redirect both, non-SSL and SSL to their non-www counterpart:

server {     listen               80;     listen               443 ssl;     server_name          www.example.com;     ssl_certificate      path/to/cert;     ssl_certificate_key  path/to/key;      return 301 $scheme://example.com$request_uri; }  server {     listen               80;     listen               443 ssl;     server_name          example.com;     ssl_certificate      path/to/cert;     ssl_certificate_key  path/to/key;      # rest goes here... } 

The $scheme variable will only contain http if your server is only listening on port 80 (default) and the listen option does not contain the ssl keyword. Not using the variable will not gain you any performance.

Note that you need even more server blocks if you use HSTS, because the HSTS headers should not be sent over non-encrypted connections. Hence, you need unencrypted server blocks with redirects and encrypted server blocks with redirects and HSTS headers.

Redirect everything to SSL (personal config on UNIX with IPv4, IPv6, SPDY, ...):

# # Redirect all www to non-www # server {     server_name          www.example.com;     ssl_certificate      ssl/example.com/crt;     ssl_certificate_key  ssl/example.com/key;     listen               *:80;     listen               *:443 ssl spdy;     listen               [::]:80 ipv6only=on;     listen               [::]:443 ssl spdy ipv6only=on;      return 301 https://example.com$request_uri; }  # # Redirect all non-encrypted to encrypted # server {     server_name          example.com;     listen               *:80;     listen               [::]:80;      return 301 https://example.com$request_uri; }  # # There we go! # server {     server_name          example.com;     ssl_certificate      ssl/example.com/crt;     ssl_certificate_key  ssl/example.com/key;     listen               *:443 ssl spdy;     listen               [::]:443 ssl spdy;      # rest goes here... } 

I guess you can imagine other compounds with this pattern now by yourself.

More of my configs? Go here and here.

like image 37
Fleshgrinder Avatar answered Sep 27 '22 18:09

Fleshgrinder