Rewrite all unresolvable URLs to a script using .htaccess

Another simple one.

The following code checks to see if the request is for a real directory or file and reroutes the request to “/myscript.php” if not.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ myscript.php?/$1 [L]

Take note of the the excamation point (it means “not”). To do the opposite (reroute valid directory/file requests through a script) just change the rewrite conditions (RewriteCond) to…

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

Can only think of a few situations where you’d want to do this, but I prefer to be thorough 😉

Leave a Reply

Your email address will not be published. Required fields are marked *