4

Nginx is currently doing the following:

example.com/folder

301 redirect to:

example.com/folder/

How can I prevent it from doing this?

Thanks.

Edit: Added Nginx configuration

server { server_name www.example.com; listen *:80; listen [::]:80 ipv6only=on; return 301 http://example.com$request_uri; } server { server_name example.com; listen :80; listen [::]:80; root /home/usr/www; index index.htm; location ~ .(js|css|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; } }

5
  • can you show your configuration? It's hard to help otherwise.
    – ek9
    Commented Mar 24, 2014 at 8:37
  • edited to include this. Commented Mar 24, 2014 at 8:52
  • with your configuration the behaviour your get is normal. It is because you have a folder named folder. The only way to get rid of the slash at the end would be to force all request to go through 1 file (e.g. index.php) and then make your application routing logic work through that. But I think this is overkill. Can you comment more on what exactly you want to achieve with this or solve?
    – ek9
    Commented Mar 24, 2014 at 8:57
  • I would rather the web server returned a 200 when a folder structure was navigated without a trailing slash. Otherwise I seem to be serving an excessive amount of 301's for legitimate URLs. Commented Mar 24, 2014 at 9:13
  • It's not a "legitimate url" really, as when you try to reach domain.com/file it first tries to access filename file and when it find directory, it redirects to file directory and looks for default (usually index.html) file. If you do not want 301 redirect, then you have to point your links to domain.com/folder/. Otherwise - you can only achieve what you want programically and not only with nginx configuration update. As you would need a a single point controlling all the routing logic. This is how it is usually done.
    – ek9
    Commented Mar 24, 2014 at 9:16

2 Answers 2

1

With your configuration the behavior your get is normal. It is because you have a folder named folder, apache prepends the trailing slash / and hides index.html (or index.php ) if that is not specified.

The only way to get rid of the slash at the end would be to force all request to go through 1 file (e.g. index.php) and then make your application routing logic work through that. But this is too much specific to what you are trying to accomplish to answer.

1

Probably that is happening during the internal rewrite phase. To disable that, add into your server (or its inner) block:

 rewrite ^(.+)/+$ $1 permanent;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.