Trailing Slash Redirect Loop Help

Alex Med nginx-forum at forum.nginx.org
Fri Apr 28 14:27:12 UTC 2017


Hi,

I am having an issue getting rid of the trailing slashes for directories.  I
have used the following to get rid off the trailing slash:


#rewrite all URIs without any '.' in them that end with a '/'         
        #rewrite ^([^.]*)/$ $1 permanent;

&
#rewrite all URIs that end with a '/'  
rewrite ^/(.*)/$ /$1 permanent;

They both work, but they do not when it comes to directories.  When it is a
directory the page is not displayed because it has different redirects. 
Here is the code that I have used inside the LOCATION definition as well as
in the SERVER block and they just do not work.


if (!-e $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}

Or, I have used this one:

if (!-e $request_filename) {
rewrite ^([^.]*)/$ $1 permanent;
}

Note:  I have tested individually the two rules either in the server block
or the location block and I still get the redirection problems with files or
directories.


Could any of you please help me and tell me what I am doing wrong. 
Basically, when I enable the rewrite rule to get rid off the trailing slash
all directories get multiple redirects and end up in a loop.

Here is the nginx configuration.


user www-data;
worker_processes X;
pid /xxxx.pid;

events {
	worker_connections xxxx;
	# multi_accept on;
}



http {
        ##
        # Basic Settings
        ##

        sendfile off;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        #below hides the version of nginx 
	server_tokens off;

        server_names_hash_bucket_size 64;
        #this sets the url hash map table size for url rewrites
        map_hash_bucket_size 128;
        map_hash_max_size 2048;
	# server_name_in_redirect off;

        include /xxxx.types;
        default_type application/octet-stream;

     

        ##
        # Gzip Settings
        ##

        gzip on;

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 7;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
 	# Disable for IE < 6 because there are some known problems
        gzip_disable "MSIE [1-6].(?!.*SV1)";

        gzip_types
		text/plain
                text/css 
		text/javascript
		application/javascript
	        application/json
		application/x-javascript
		application/xml;

        ##
        # Buffer Size
        ##
        proxy_buffering on;
        proxy_buffers   4 256k;
        proxy_buffer_size   128k;
        proxy_busy_buffers_size   256k;

     


        ##
        ## URL REWRITE MAP
        ## File that contains all url rewrite rules for server
        include xxxurlmap.conf;


# SERVER DEFINITIONS




###FORWARD ALL 80 INCOMING REQUEST TO SSL
server {
	listen 80 default_server;
	listen [::]:80 default_server;
	server_name example.com www.example.com;
	return 301 https://www.example.com$request_uri;
}

#HTTPS server

#CHANGE DOMAIN NAME from NO-WWW to WWW - CREATE ONE SERVER INSTANCE AND
RETURN IT
server {
    listen       443 ssl http2;
    server_name  example.com;
    ssl_certificate     /xxx.pem;
    ssl_certificate_key /xxxx.pem;
   return       301 https://www.example.com$request_uri;
}


# Secure Server Configuration HTTPS Server

server {

        listen 443 ssl http2;
        listen [::]:443 ssl http2;        
        server_name www.example.com;

 
        ssl_certificate     /etc/ssl/fullchain.pem;
        ssl_certificate_key /etc/ssl/privkey.pem;
	    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        ssl_session_cache shared:SSL:20m;
        ssl_session_timeout 60m;
	
     
 
       ## THIS CONDITION REWRITES ALL URLS TO THE NEW ONES
        if ( $redirect_uri ) {
        return 301 $redirect_uri;
           }
   

        location / {
            proxy_pass https://xxxx:xportNumber;
            proxy_set_header  Host $host;
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;
   #this deletes all the traling slash of all Content 
   if (!-e $request_filename) {
        rewrite ^/(.*)/$ /$1 permanent;
         }


                } 


        }

}

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273964#msg-273964



More information about the nginx mailing list