mod_dir and internal rewrites

firebus's picture

Oy.

In an htaccess, I wanted to make some internal rewrites for a directory using mod_rewrite

It would have been better to use mod_alias for this, but you can't use Alias in a Directory/htaccess context, and I had no access to the vhost configuration.

Here's the first rewrite I tried:

RewriteRule ^some/path(.*) filesystem/directory$1

This worked and provided an internal rewrite for URLs like
- some/path/something
- some/path/

But when the trailing slash was missing for /some/path instead of an internal rewrite I got a 301 redirect to filesystem/directory/ instead. The content was correct, but I really wanted the behavior of an Alias and not a Redirect.

I looked everywhere in the configuration chain for a matching RewriteRule or Redirect that could be catching this and adding a redirect with no luck.

I turned on RewriteLog, and I didn't see the 301 there either! Only the internal rewrite from some/path to filesystem/directory

Finally I noticed that the 301 redirect was not to filesystem/directory, but instead to /filesystem/directory/ with a trailing slash.

Where was that trailing slash coming from?

After checking the included configs for mod_alias and mod_rewrite, I decided to take a look at mod_dir for some reason...

http://httpd.apache.org/docs/2.2/mod/mod_dir.html


Description: Provides for "trailing slash" redirects and serving directory index files
...
A "trailing slash" redirect is issued when the server receives a request for a URL http://servername/foo/dirname where dirname is a directory. Directories require a trailing slash, so mod_dir issues a redirect to http://servername/foo/dirname/.
...
DirectoryIndex will be evaluated only for directories requested with trailing slash.

Oh, okay then :( mod_dir was issuing the redirect, it was getting invoked after mod_rewrite, and it wasn't smart enough to play nice.

I ended up adding my own redirects to add the trailing slash onto old/path.

I'm not sure if I would have run into this if old/path was also a directory on the filesystem or if RewriteBase (which was set) also played a part in this?


Powered by Drupal - Design by Artinet - Amazon Affiliate