: Transparent PHP script execution using mod_rewrite 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
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.
More posts by @Jamie184
3 Comments
Sorted by latest first Latest Oldest Best
Solution:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ script.php?filename= [L]
make script.php run your code and afterwards redirects using the filename variable you passed to it. ($_GET['filename'])
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)
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
oursite.com/special
Which didn't exist, so it would die to the 404. My switch/case looked for "special" and could redirect to...
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.