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
try this
if ($host !~* ^www\.){
rewrite ^(.*)$ https://www.yoursite.com$1;
}
Other way: Nginx no-www to www
server {
listen 80;
server_name yoursite.com;
root /path/;
index index.php;
return 301 https://www.yoursite.com$request_uri;
}
and www to no-www
server {
listen 80;
server_name www.yoursite.com;
root /path/;
index index.php;
return 301 https://yoursite.com$request_uri;
}
in order to make nginx recommended method with return 301 $scheme://example.com$request_uri;
work with Ghost you will need to add in your main server block:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_pass_header X-CSRF-TOKEN;
proxy_buffering off;
proxy_redirect off;
Redirect non-www to www
For Single Domain :
server {
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
For All Domains :
server {
server_name "~^(?!www\.).*" ;
return 301 $scheme://www.$host$request_uri;
}
Redirect www to non-www For Single Domain:
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
For All Domains :
server {
server_name "~^www\.(.*)$" ;
return 301 $scheme://$1$request_uri ;
}
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;
...
}
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!
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;
}
}
You need two server blocks.
Put these into your config file eg /etc/nginx/sites-available/sitename
Let's say you decide to have http://example.com as the main address to use.
Your config file should look like this:
server {
listen 80;
listen [::]:80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name example.com;
# this is the main server block
# insert ALL other config or settings in this server block
}
The first server block will hold the instructions to redirect any requests with the 'www' prefix. It listens to requests for the URL with 'www' prefix and redirects.
It does nothing else.
The second server block will hold your main address — the URL you want to use. All other settings go here like root
, index
, location
, etc. Check the default file for these other settings you can include in the server block.
The server needs two DNS A records.
Name: @ IPAddress: your-ip-address (for the example.com URL)
Name: www IPAddress: your-ip-address (for the www.example.com URL)
For ipv6 create the pair of AAAA records using your-ipv6-address.