0

I have been looking to rewrite my api URLs so they don't have a trailing slash. I use NGinx and not Apache. I did find this answer to the same question for an Apache server, but it is not going to work out of the box for NGinx.

Alexis Evelyn
  • 304
  • 5
  • 17

1 Answers1

0

I ended up taking this sample Apache config and used a config rewrite service to convert the config to Nginx format. It produces a mostly working, but slightly broken solution. It accesses the URLs like it is supposed to, but it breaks accessing resources and causes the /api/index.php to be dumped as a download when accessing a non-existent file or directory in /api/.

I played around with the config to produce the below config which uses the html 404 message provided to the whole server.

NGinx

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

My File structure is:

/api/index.php
/api/hotbits/index.php
/api/cryptography/index.php

With the new config option, this translates to these functional URLs:

/api
/api/
/api/hotbits
/api/hotbits/
/api/cryptography
/api/cryptography/
Alexis Evelyn
  • 304
  • 5
  • 17