Apache Adds Trailing Slash Despite RewriteRule

.htaccessapache-2.4mod-rewrite

I'm using a simple .htaccess to handle everything with a PHP script:

RewriteEngine on
RewriteRule . index.php

My index.php just echoes server variables at the moment (with a
print_r($_SERVER)); Everything works fine, I type anything in the URL
and the index.php contents are shown. However, considering this
structure:

test/           # document root
  folder/       # empty folder
  index.php
  .htaccess

I noticed that accessing [site]/folder first redirects the browser to
[site]/folder/ and then shows the proper index.php contents. Why is
this happening? Isn't the rewrite rule supposed to redirect everything
"as is"?

This is being used with virtual host set up to test a website locally.
Configured with the bare minimum. Could there be other apache files
being loaded prior to my .htaccess?

<VirtualHost *:80>
    ServerName test.local
    DocumentRoot /Library/WebServer/Documents/test/
</VirtualHost>

Using OS X 10.10 currently.

Best Answer

As determined in the comments above, the DirectorySlash directive controls this particular Apache behaviour and the rewriterule did not cause it. This directive defaults to on. If you set it to off, then when a URI that maps to a directory on the file system is requested without a trailing slash, Apache will not redirect the request to one with a trailing slash.

More information in the documentation here: https://httpd.apache.org/docs/current/mod/mod_dir.html#directoryslash

Related Topic