Transparent PHP script execution using mod_rewrite

Transparent PHP script execution using mod_rewrite - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about php, htaccess, mod-rewrite, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I am looking for a solution for this a problem: I need that every HTTP request (method is irrelevant) in Apache http server would be served only after execution of specific PHP script. This is needed because I need to gather some information about requests, etc.
As far as I understand - this could be achieved using mod_rewrite module. So far I have done this (in .htaccess file):



RewriteEngine on
RewriteRule ^(.*)$ script.php [C]


script.php is executed, but I need that after this original request would be executed.



Thanks - any help is appreciated.


Solution :

Solution:



Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ script.php?filename=$1 [L]


make script.php run your code and afterwards redirects using the filename variable you passed to it. ($_GET['filename'])



I've done a similar setup in the past in my 404.php page. I look at the url coming in, and if it matches something I'd expect I would use a LOCATION: header to redirect.



The purpose for this in my case was after a rewrite to the site, magazines, etc. that had linked to either deprecated sections or articles would just die at the 404. I wanted to take them to the correct content or at least a "friendlier" section.



Another use for it was to shorten urls so we could send out



www.oursite.com/special



Which didn't exist, so it would die to the 404. My switch/case looked for "special" and could redirect to...



www.oursite.com/some/place/inside/the/site/for/special/



Don't know if that helps... but it's one way to "pre-filter" and redirect traffic.



If you are working with PHP anyway, why not use the auto-prepend-file (see PHP manual) settings. To add it conditionally via .htaccess see this suggestion.



Alternatively, it should be possible to use Apache RewriteRules to call your script via RewriteMap's possibility to call external scripts. (I do not think it is a good idea to do and would not recommend it)


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect