.htaccess url-rewrite if file not exists

Kiavor picture Kiavor · Mar 29, 2011 · Viewed 42.9k times · Source

I must do a little trick for a site! The idea is:

  • if a file for a required url exists then I go to that url, doing nothing more;
  • if a file for a required url not exists, I must go to a file.php and than do something, but NOT changing the url!

example:

www.mysite.com/page1.htm -> exists -> go to file page1.htm

www.mysite.com/page2.htm -> NOT exists -> go to file default.php but with url "www.mysite.com/page2.htm"

It's possible to do this all by .htaccess?

Answer

user237419 picture user237419 · Mar 29, 2011
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /default.php [L]