0

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.

6
  • Can you clarify your question with a couple of examples: What is the exact URL and which resource you want to be served by the URL? I don't see what is the exact meaning of "add slashes to the end of all the url on my site". Sep 2, 2022 at 14:19
  • 1
    Test your uri against your regexp here regex101.com at first place
    – gapsf
    Sep 3, 2022 at 7:00
  • @TeroKilkanen Hi I just updated with example of what I am aiming for! Sep 3, 2022 at 7:07
  • @gapsf I tested the regex online already, it worked fine, the problem remaining for me is really to preserve the port (1080) because when tested locally it removes the port. And adding the directive "port_in_redirect off; " just literally made the whole local site inaccessible. Sep 3, 2022 at 7:10
  • 1
    I still don't see what do you mean by "add slashes to the end of the URL". Do you want to have HTTP redirect from without slash to with slash version of the URL? Do you want to have internal nginx redirect? Or do you want something else? Also, adding slash to the end of query arguments part of URL like in your third example makes no sense. Sep 3, 2022 at 7:35

0

You must log in to answer this question.

Browse other questions tagged .