soiler Posted November 1, 2011 Share Posted November 1, 2011 I apologize ahead if this is really an Apache question, but I suppose it could be in either forum. I have a web directory that when anything is accessed within it or its children from a range of IP addresses, they are redirected to a new URL, like so: .htaccess Options +FollowSymlinks RewriteEngine On RewriteCond %{REMOTE_ADDR} ^111\.111\.111\. [OR] RewriteCond %{REMOTE_ADDR} ^222\.222\.222\. RewriteRule !^(.*redirects.*)$ http://www.somedomain.com/something.php [L,NC,R] something.php <?php $to = "Someone <[email protected]>"; $subject = "Some Subject"; $headers = 'From: Somebody <[email protected]>' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $VisitorIp=$_SERVER['HTTP_X_FORWARDED_FOR']; else $VisitorIp=$_SERVER['REMOTE_ADDR']; trim($VisitorIp); $body = "Someone of IP " . $VisitorIp . " accessed resource\n"; mail($to, $subject, $body, $headers); ?> The problem is that I do not know on how to get the specific resource (URL) the client tried to access into the email, which I'd also like (e.g. more referrer information). The IP address is no problem though. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/ Share on other sites More sharing options...
requinix Posted November 2, 2011 Share Posted November 2, 2011 Don't use an [R] flag in the rewriting, and have your script look at $_SERVER["REQUEST_URI"]. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1284129 Share on other sites More sharing options...
soiler Posted November 3, 2011 Author Share Posted November 3, 2011 Thanks for your help requinix. Two things. What does the [R] flag do? I couldn't seem to find it in the Apache documentation (which is pretty intimidating). The REQUEST_URI variable just seems to always contain the URI of the page being redirected to, not the URI that the client originally tried to access before being redirected. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1284461 Share on other sites More sharing options...
requinix Posted November 4, 2011 Share Posted November 4, 2011 Normally Apache will do URL rewriting in the background - the client doesn't know anything is happening. The REQUEST_URI will be whatever the request was originally for, but the script that actually runs will be whatever the rewritten URL referenced. The [R] flag means to redirect instead of rewrite. The client gets redirected to the new URL and so it submits a new request. Then the REQUEST_URI will be whatever it was for the new request. So remove the [R] and you should be good. As for documentation, the two things in mod_rewrite you'll use most are RewriteCond and RewriteRule. It helps to know how HTTP works but a quick read-through of the documentation for those two directives should get you what you need. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1284886 Share on other sites More sharing options...
soiler Posted November 9, 2011 Author Share Posted November 9, 2011 Hey requinix. Even after removing the [R] flag, the REQUEST_URI variable still only contains the new URL, not the one the client had originally intended to access. Thanks for your help in this. It's been driving me nuts for a long time now. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1286467 Share on other sites More sharing options...
requinix Posted November 9, 2011 Share Posted November 9, 2011 Huh. Maybe I have it backwards... Somewhere in $_SERVER is the original URL. Maybe something like REWRITE_URI? Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1286469 Share on other sites More sharing options...
soiler Posted November 9, 2011 Author Share Posted November 9, 2011 REWRITE_URI doesn't seem to contain anything. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1286473 Share on other sites More sharing options...
requinix Posted November 9, 2011 Share Posted November 9, 2011 Okay, then that's not it. Look for something else. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1286702 Share on other sites More sharing options...
soiler Posted November 10, 2011 Author Share Posted November 10, 2011 Any suggestions? Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1286970 Share on other sites More sharing options...
aliento Posted November 10, 2011 Share Posted November 10, 2011 As it seems it cannot be done. I think you can take the previous page with history javascript. Or you can use sessions to save and retrieve the URI. For those purposes i include the file and i am not redirecting the page. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1286973 Share on other sites More sharing options...
soiler Posted November 11, 2011 Author Share Posted November 11, 2011 Hey aliento. It sounds like those methods are beyond my present capabilities and so for now it looks like I'm out of luck. Thanks for your help anyways though. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1287228 Share on other sites More sharing options...
xyph Posted November 11, 2011 Share Posted November 11, 2011 You could try using print_r( $_SERVER ); to see if any of those contain the right URL you're looking for. If I have time later I'll look into this. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1287230 Share on other sites More sharing options...
soiler Posted November 11, 2011 Author Share Posted November 11, 2011 Hey xyph. $_SERVER["HTTP_REFERER"] sometimes seems to have what I need. Thanks for the suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/250258-email-notification/#findComment-1287245 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.