0

I spent about 20 minutes wrapping my head around this, and finally came to a solution but did not know why it worked. The important lines (as it relates to this question) in the apache configuration file looked like this:

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^$ /abc [L]

When I tried accessing http://example.com, the last rewriterule is skipped but what I do not understand is how is nothing a directory?

Based on the last three items from the way I see it, I'm checking to see if I'm requesting a file or a folder, and if not, then redirect nothing (aka the default page) to /abc.

It turns out here that RewriteCond %{REQUEST_FILENAME} !-d returns false because when I remove it and try again using the configuration shown below:

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^$ /abc [L]

the redirection then works.

Is there a reason as to why "nothing" counts as a directory? I don't want to fall into any more illogical traps like this as I update my website.

3
  • I read this and am not sure I understand why "nothing" counts as a directory except if example.com results in example.com/ which is totally normal and required. The URL is example.com and the URI is the path and file. If the URI is blank, the assumption is /. The browser adds the / URI if no URI exists. However, if example.com/abc is used, abc cannot be known to the browser and so it is passed where the server searches for the first match (directory) and a / is added. Is this what you are looking for??
    – closetnoc
    Commented Nov 21, 2015 at 16:26
  • ok it makes sense. so nothing equals a / Commented Nov 21, 2015 at 19:49
  • Yeppers! I gotta type 6 more characters but by now I have overshot that by quite a bit.
    – closetnoc
    Commented Nov 22, 2015 at 0:32

1 Answer 1

1

Becuase you are not checking that nothing is a directory, you are checking whether REQUEST_FILENAME is a directory (or not in this case).

Whilst the URL-path, that matches against the RewriteRule pattern, is nothing (after the directory-prefix is removed), the REQUEST_FILENAME (which contains the absolute filesystem path that the request maps to, ie. not a URL) will contain something like /home/username/public_html/ (depending on your config). This is a directory on the local filesystem, so the above RewriteCond evaluates to false.

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.