Welcome! Log In Create A New Profile

Advanced

Trailing Slash Redirect Loop Help

April 28, 2017 10:27AM
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;
}


}


}

}
Subject Author Posted

Trailing Slash Redirect Loop Help

Alex Med April 28, 2017 10:27AM

Re: Trailing Slash Redirect Loop Help

steveh April 28, 2017 10:42AM

Re: Trailing Slash Redirect Loop Help

Alex Med April 28, 2017 10:48AM

Re: Trailing Slash Redirect Loop Help

steveh April 28, 2017 11:36AM

Re: Trailing Slash Redirect Loop Help

Alex Med April 28, 2017 11:43AM

Re: Trailing Slash Redirect Loop Help

steveh April 28, 2017 11:48AM

Re: Trailing Slash Redirect Loop Help

Alex Med April 28, 2017 12:52PM

Re: Trailing Slash Redirect Loop Help

steveh May 01, 2017 06:42PM

Re: Trailing Slash Redirect Loop Help

Alex Med May 10, 2017 10:43AM

Re: Trailing Slash Redirect Loop Help

Francis Daly April 29, 2017 08:36AM

Re: Trailing Slash Redirect Loop Help

Alex Med May 10, 2017 11:10AM

Re: Trailing Slash Redirect Loop Help

Francis Daly May 12, 2017 05:26PM

Re: Trailing Slash Redirect Loop Help

Alex Med October 06, 2019 12:24PM

Re: Trailing Slash Redirect Loop Help

Francis Daly October 08, 2019 06:48PM

Re: Trailing Slash Redirect Loop Help

Alex Med October 09, 2019 11:22AM

Re: Trailing Slash Redirect Loop Help

Francis Daly October 09, 2019 05:34PM

Re: Trailing Slash Redirect Loop Help

Alex Med October 14, 2019 11:04PM

Re: Trailing Slash Redirect Loop Help

Alex Med October 14, 2019 11:06PM

Re: Trailing Slash Redirect Loop Help

Francis Daly October 15, 2019 10:36AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 302
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready