1

I am building a .htaccess file with a fairly complex ruleset.

One of the basic rewriting rules is this first line:

If a requested file exists physically, no rewriting, stop processing rules.

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]

this works well with one exception: When I access a directory with a trailing slash

www.example.com/directory/

directory will not be recognized as existant, whereas

www.example.com/directory

will.

What can I do to make Apache recognize existing directories with a trailing slash as well?

Background info, I am already adding trailing slash to every request made without one (to cater for both /directory and /directory/ requests.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • To be precise: `/foobar/` does not refer to the directory named *foobar* but to the index document in the directory *foobar*. Apache is doing a subrequest if `/foobar/` is requested. Only `/foobar` does refer to the directory named *foobar*. – Gumbo Feb 08 '10 at 13:20
  • @Gumbo good point. So I'll probably have no choice but to shave off the trailing slash in internal processing. Will try that. – Pekka Feb 08 '10 at 13:26
  • You can use mod_rewrite’s logging feature (see `RewriteLogLevel`) to see how your requests are processed. – Gumbo Feb 08 '10 at 13:31

1 Answers1

1
RewriteCond %{REQUEST_FILENAME} -d

The -d stands for directory. Also, according to Hidden features of mod_rewrite , the [L] directive doesn't work in .htacess files, just if you put it in your apache conf files.

Also if you simply start off with:

RewriteEngine on
# Dont rewrite files or directories which actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

You can avoid rewriting any existing files or directories. Your existing rules says "If this is a real file, keep going" whereas mine says "If its not a real file and its not a real directory, keep going."

Community
  • 1
  • 1
Erik
  • 20,526
  • 8
  • 45
  • 76
  • Brilliant, thanks a lot. [L] not working in .htaccess explains a lot too. I'm off for now, will test this tomorrow. – Pekka Feb 08 '10 at 01:41
  • Can you post your entire .htaccess? The second group of lines I posted starting with `RewriteEngine on` is exactly what i use to redirect any file or directory which does not exist to a specific file (with the unshown rewrite rule). – Erik Feb 08 '10 at 21:08