How to remove www from URL with nginx server

Nginx web server is becoming most popular in these year. Best server response time and minimum execution time is the advantages of Nginx server. wordpress.com itself using the nginx server for hosting these sites and blogs.

How to remove www from URL with nginx server

How to remove www from URL with nginx server
How to remove www from URL with nginx server

How to remove www from URL with nginx server

Many people want to remove the www from URL. I will tell you the tip how to remove the www from URL with nginx server.

Open your Ngnix configuration file (nginx.conf). Just use following code in server block.

if ($host ~* ^www\.(.*)) {
    set $remove_www $1;
    rewrite ^(.*)$ http://$remove_www$1 permanent;
}

This code will remove the www from the URL.

Here are some useful article about nginx

How to use hosts file on Mac, Windows and Linux
Solved:trailing slash issue with Nginx server

Solved trailing slash issue with Nginx server

I added following block in my nginx.conf file. You should add following lines in your server { } block.

We Solved trailing slash issue with Nginx server

If you want use URL without ending trailing slash. then use following code.
#code block added for trailing slash issue solving
server_name_in_redirect off;
optimize_server_names off;
dav_methods PUT DELETE MKCOL COPY MOVE;
dav_access group:rw all:r;
create_full_put_path on;

if (-d $request_filename) { rewrite ^(.*[^/])$ $1/ break; }
if ($request_method = MKCOL) { rewrite ^(.*[^/])$ $1/ break; }
Above code will check if folder is present then fetch data from there. If you have some idea about MKCOL then you will know above code in better way.
If Your problem will not solved from above code then use following code.
#here we are adding trailing slash end of Url
if ($request_uri ~* “^[\w\-\/]+[^\/?]$”) {rewrite ^(.*)$ $scheme://$host$1/ permanent;}
This will add the ending slash to your url.