Jade
Jade

Reputation: 649

Mod_rewrite to 404 page if match not found

Hey I'm trying to do a mod-rewrite for search engine friendly URLs that resolve to a 404 page if the file isn't found. When I get it working for directories that don't resolve to a file (ie search/.php) because no match is found then it doesn't work when a match is found.

Basically I need it to work when you go to url/search/ if there is an index page and url/search/filename should resolve to filename.php inside of the search directory. Anyone know how to accomplish this?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [S2] #file not found, skip to 404
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule .? - [S1] #file found, ignore redirect to 404
RewriteRule (.*) 404.php?file=$1

Upvotes: 1

Views: 2429

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Why not just:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) 404.php?file=$1 [L]

instead of all of those skips?

Upvotes: 2

Related Questions