Rick
Rick

Reputation: 3

How to check if file exists in Apache

I've set this up successfully in IIS in the past, but i'm not sure how to do it in Apache. I want to check to see if a file exists, whether it be:

/path/to/file.php
/path/to/file.cfm
/path/to/file.html
/path/to/

If the file does not exist, I want to redirect to /404/, where I have a file that does all 404 related handling.

Lastly, when doing this in IIS, it gave me a CGI.QUERY_STRING of something like:

404;http://example.com/prettyurl

Can apache do the same?

This is what i've tried to so far, to no success. (I'm probably way off here...)

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} -f
RewriteRule (.*)$ /404/ [R,L,NC]

Thanks..

Upvotes: 0

Views: 2951

Answers (1)

Mikulas Dite
Mikulas Dite

Reputation: 7941

Don't use the document_root slash..., simply do REQUEST_FILENAME !-f Exclamation mark negates the -f flag.

Upvotes: 1

Related Questions