views:

47

answers:

2

I am in need of the rewrite rules and conditions to evoke a central PHP script for every and any web document requested on my server. Just to add, I do not want the PHP script to be evoked when requests are made for CSS, JS, PNG, GIF, JPG, and other media etc. I only want the script to be called for HTM, HTML and PHP documents in the root and in any directory or subdirectory.

The main stumbling point I am having is I cannot get the script to be evoked for any of the assumed directory indexes. Here is what I am getting:

  • http://sampleDomain.com/test/index.html (follows the RewriteRule and evokes myScript.php)
  • sampleDomain.com/test/test.html (follows the RewriteRule and evokes myScript.php)
  • sampleDomain.com/test/ (does not follow the RewriteRule - it displays a directory listing)
  • sampleDomain.com/test (does not follow the RewriteRule - it displays a directory listing)

Here are my directives:

  • RewriteEngine On
  • RewriteCond %{REQUEST_FILENAME} !-f [NC,OR]
  • RewriteCond %{REQUEST_FILENAME} !-d [NC]
  • RewriteRule . /phpDir/myScript.php [L]

What does the RewriteRule and RewriteCond look like to get every/any web document to evoke myScript.php?

Thanks.

A: 

Found it through persistent searching and testing:

RewriteEngine on
RewriteRule ^(.*)$ /phpDir/myScript.php [L]

Dr. DOT
A: 

Try this on for size:

DirectoryIndex /phpDir/myScript.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !phpDir/myScript.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(php|html|htm)$ /phpDir/myScript.php [L]

Breaking it down

DirectoryIndex /phpDir/myScript.php

Sets the default directory file to be that of your script. So if you enter a folder that doesn't have index file, use /phpDir/myScript.php

RewriteCond %{REQUEST_FILENAME} !phpDir/myScript.php [NC]

Checks to make sure you're not requesting your central PHP script. Otherwise you could find yourself in a never-ending loop.

RewriteCond %{REQUEST_FILENAME} !-f

Check that the file you're requesting isn't already an existing file. In case you want to have other files instead of pushing everything onto your central script.

RewriteRule ^(.*)\.(php|html|htm)$ /phpDir/myScript.php [L]

This last rule checks to see if the requested file is of one of the file extensions (in this case .php, .htm or .html) and if it is, pass it to the central PHP script. This will leave alone other files, such as graphics, CSS and other media.

random
Thanks "Crisis of Indentity".I tried it in a .htaccess file in one directory on my server and it worked. I need to do some server-wide cleanup before I can place this into my httpd.conf file and try on the entire server, but tanks for the solution and your step-by-step explanations -- it really helps.FYI...I did get a warning message however:[Tue Nov 24 07:50:10 2009] [warn] RewriteCond: NoCase option for non-regex pattern '-f' is not supported and will be ignored.So I am removing the [NC] option from the RewriteCond line being referenced.
Dr. DOT
Thanks, totally missed the warning about the NC sitting right there in the error log. Edited to clear up/out.
random