Apache and Nginx Configurations

· Code Samples

These are the configurations I recommend for the The Trailing Slash Solution. In summary of the proposal:

  • Static resources (files) should not have a trailing slash
  • Dynamic resources (pages via index.php) should have a trailing slash
  • Directories should have a trailing slash, but no index
  • Dynamic applications (with an index.php entry point) can be placed in a sub directory with own configurations
  • All requests that are not for a file will be rewritten through index.php

I’ve tested these configurations against each other using a cURL library I built and they behave very similar. You can view the tests here: Apache Test Results and Nginx Test Results. The big difference between Nginx and Apache when using the configurations is when handling a file that exists, but the URL includes a trailing slash. Apache recognizes it as a file, but will return a 404 so there is a rewrite rule below that removes the trailing slash. Nginx on the other treats the file as non-existent so there is no rewrite rule to remove the trailing slash. Apache was used as the baseline so any FAIL result on the Nginx Test is because Nginx behaves differently than Apache. The tests were conducted on Vagrant hashicorp/precise32 with Apache/2.2.22 (Ubuntu) and Nginx/1.1.19 using PHP/5.3.10-1ubuntu3.10.

When a URL is requested, the web server will check for the existence of a file. If the file is not found, it will forward the request to /index.php for further routing. Any direct requests for the root /index.php will be redirected to /. These configurations are case insensitive, but that doesn’t matter if the file system is case sensitive. If you request /favicon.ico on a Linux system and it is actually called /Favicon.ico, it will return a 404.

Note: The Apache and Nginx regular expressions are as close as possible to each other, but must be a little different because the Apache request URI does not include the leading slash or query string while the Nginx request URI does. When there is a redirect, Apache strips multiple slashes while Nginx does not.


If you are using Apache, place an .htaccess file in the root of you web folder with the following configuration. You can comment out any pieces you don’t want to use.

Make sure you set AllowOverride to Allow in your apache2.conf or httpd.conf so .htaccess will be processed. The flag [NC] means case insensitive.

If you want to place another application in a sub folder, here is the sub folder configuration. Change the 4 instances of test to your sub folder name.

I also tested the CheckSpelling and CheckCaseOnly directives, but they don’t seem to work if everything is rewritten to /index.php.


For Nginx, you’ll need add the pieces you want into the server block of your nginx.conf file.

The flag ~* means case insensitive.

If you want to place another application in a sub folder, here is the sub folder configuration. Change the 6 instances of test to your sub folder name and add above the first location block in your nginx.conf file.

author image

About Joseph Spurrier

I'm passionate about building solutions to automate tasks and improve efficiency. I worked for a few companies in the digital healthcare space. My education is in forensics. Tech guy. Traveler. Runner. Guitar player. Scotch and beer enthusiast. GitHub LinkedIn Twitter Google+

Share this post

Comments powered by Disqus