Mobile app version of vmapp.org
Login or Join
Jamie184

: 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

@Jamie184

Posted in: #Htaccess #ModRewrite #Php

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.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Jamie184

3 Comments

Sorted by latest first Latest Oldest Best

 

@Michele947

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'])

10% popularity Vote Up Vote Down


 

@XinRu657

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)

10% popularity Vote Up Vote Down


 

@Vandalay111

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme