I am trying to add slashes to the end of all the url on my site except for files with extensions (all images type, css, js, xml), all while preserving the port for my local env.
This is the behavior I am looking for:
In production:
https://testsite.com/page/something → https://testsite.com/page/something/
https://testsite.com/page/something/img.jpg → https://testsite.com/page/something/img.jpg
https://testsite.com/page/something?param=23 → https://testsite.com/page/something?param=23/
Local environment
localhost:1080/page/something → localhost:1080/page/something/
localhost:1080/page/something/img.jpg → localhost:1080/page/something/img.jpg
localhost:1080/page/something?param=23 → localhost:1080/page/something?param=23/
I have been trying to do it by adding:
if ($request_uri ~ ^(.*)(\/(?!jpg|gif|png|jpeg|xml|css|js)([^.\/])+)$) {
try_files $uri $uri/ /index.php?$query_string/;
}
inside the
location /
statement and for the port, I wrote the
port_in_redirect off;
directive.
The complete config is as below:
server {
listen 80;
server_name _;
root /var/www/html/public;
index index.html index.htm index.php;
charset utf-8;
client_max_body_size 100m;
port_in_redirect off;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
gzip on;
gzip_types text/plain text/css application/javascript text/javascript image/svg+xml image/png image/gif application/font-woff application/xml application/json application/octet-stream;
gzip_proxied any;
gzip_vary on;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location / {
if ($request_uri ~ ^(.*)(\/(?!jpg|gif|png|jpeg|xml|css|js)([^.\/])+)$) {
try_files $uri $uri/ /index.php?$query_string/;
}
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass web:9000;
fastcgi_read_timeout 300;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
However now, it seems the site doesn't load at all. I would like to know what is the best way to do this. Thanks in advance.