What does this mean: RewriteCond %{REQUEST_FILENAME} !-f/d?

What does this mean:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

I found this in the .htaccess file of Mambo.

All I know is that if you use RewriteRule ^(.*) index.php, it will redirect everything entered from http://www.sample-domain.com to index.php so that you can parse the URI. But what about the rest of the code?

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

What do they do?

I found something similar about my question at http://www.sitepoint.com/forums/showthread.php?t=162537:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %{REQUEST_FILENAME} [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^ %{REQUEST_FILENAME}/index.php [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^ 404/ [L]

RewriteRule ^(.*[^/]) index.php?var=$1 [QSA,L]

I only know the first two lines. frowning

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

... means that if the file with the specified name in the browser doesn't exist, or the directory in the browser doesn't exist then procede to the rewrite rule below smile

Thanks Dean, what about the other one? What does it mean? And which one is better?

DirectoryIndex index.php
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %{REQUEST_FILENAME} [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^ %{REQUEST_FILENAME}/index.php [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^ 404/ [L]

RewriteRule ^(.*[^/]) index.php?var=$1 [QSA,L]

Q,

Dean's correct - what does it matter "which one is better?"

DirectoryIndex index.php
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %{REQUEST_FILENAME} [L]
# invalid code - and useless if the ^ and % were joined

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^ %{REQUEST_FILENAME}/index.php [L]
# invalid code as the second condition can NEVER happen and the Rule is flawed (regex of ^ !!!).

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^ 404/ [L]
# ditto
RewriteRule ^(.*[^/]) index.php?var=$1 [QSA,L]

My advice is to go to the Apache website and read about mod_rewrite. You can get an overview of this at datakoncepts.com/seo and it'll have a couple of links at the bottom for you, too.

Regards,

DK

Okay, I'll visit the website. smile

Okay, I've read it. It's similar to an old article I've read here at SitePoint. So which one do you think is better? The code I've posted in my first post or the one at http://www.datakoncepts.com/seo ?

Q,

Actually, what you're trying to do in your first code block is to redirect 404s to index.php. Anything other than

ErrorDocument 404 /index.php

is overkill and NOT necessary. In other words, when there's a specific "function" to do what you want, it's silly to write three lines of regex and make Apache run another module.

Regards,

DK

So among the three codes, the ErrorDocument is much better?

Wouldn't using ErrorDocument clog the error log as compared to using the other one?

Q,

  1. Yes.

  2. Don't you want to know when you (or some other idiot) is trying to link to a non-existant script? I'd say it won't "clog the log" and, if it does, so much the better as you'll then know that you have a problem and can resolve it quickly.

Regards,

DK

Someone posted from the PHP mailing list that I should use header ("HTTP/1.0 200 OK");

Q,

What was the question they were answering? That's what you'd do if you wanted the visitor to get a server response that all was okay (which it would when serving a file - yes, even with the ErrorDocument redirect 'cause the page served is properly served). In other words, it may be the right answer but ONLY to a different question.

Regards,

DK

From: php-db@lists.php.net

a 404 script is the way I would handle it.

Modify what the server does when it finds a page that doesn't exist.
Currently the server issues 404 headers and displays a brain dead message
about not being able to find the page. Change that so that the server
issues 200 headers (tricking the client which has "friendly error message"
enabled) and pick off the part after the .com/ and do a look up for the
item. If found, proceed as expected, it not found display, a brain dead 404
message.

How do you modify the server?
Either modify the 404 directive in Apache or one of the other lesser
products
or
setup a .htaccess file on their domain

The .htaccess has a line that says:
ErrorDocument 404 /new.php

Which means when the page is not found display the page /new.php

Build your product look up and place it in the file new404.php

Be sure that the first line of new.php is
<?header("HTTP/1.0 200 OK");

That's the header that tricks the "Friendly Error Message"

I wish I had come across this info as easily as you just did!

Mark Cain

Q,

A big to do about nothing. Just use the ErrorDocument as it'll redirect and take care of the headers for you (as above). Apache's pretty smart - let it do it's thing.

Regards,

DK

Okay. I'll just use ErrorDocument then add the header() OK status. smile

BTW, I added a reputation to both of you. smile Thanks for the help.

blush

Regards,

DK