开发者

mod_rewrite rules for all web documents

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 index开发者_高级运维es. 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.


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.


Found it through persistent searching and testing:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜