hunter
hunter

Reputation: 940

htaccess - test a rewritten url

I have the following code which takes a url like 'www.example.com/foo' and rewrites it to 'www.example.com/html/foo.php'

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([^\.]+)$ html/$1.php [NC]

I want to modify this such that if the rewritten url: 'www.example.com/html/foo.php' doesn't exist, the url is rewritten to 'www.example.com/html/fallback.php?query=foo'

I've been going around in circles for hours. Any solutions?

Upvotes: 1

Views: 252

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

You can try and do the file exists check first (using -f) then afterwards, do the rewrite that you have:

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} ^([^\.]+)$
RewriteCond %{DOCUMENT_ROOT}/html/%1.php !-f
RewriteRule ^ html/fallback.php?query=%1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ html/$1.php [L]

Upvotes: 3

Related Questions