Forum Moderators: phranque

Message Too Old, No Replies

I want to temporarily redirect everyone but me

How to redirect with .htaccess, grab original request later from PHP

         

MatthewHSE

5:43 pm on Jan 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may belong in the Apache or PHP forum, but it's such a mix of the two that I thought this would be the best place to ask! ;)

I'm trying to write a redirect script to put up briefly while I'm doing some routine maintenance on my site. I want to redirect all visitors except for me to a "Routine Maintenance" notice. I'm assuming I'll need to use .htaccess to redirect all but my IP to the notice page, but I don't know how to use IP conditionals in .htaccess.

The second step will be handling the page requests from visitors other than me. The .htaccess file will have redirected them to a PHP script. I want that to show them the URL of the page they were trying to reach, then provide a form where they can enter their e-mail address to be notified when the page becomes available. I can handle all this scripting, except determining what the original request was. I tried using the $REQUEST_URI variable, but it always returns as the URL for the page the .htaccess file redirected me to. I need some way of grabbing the URL for the page the visitor wanted to go to before .htaccess sent them somewhere else.

If anyone can shed some light on these questions, I will be most grateful!

Thanks,

Matthew

mipapage

5:55 pm on Jan 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One trick I use for this is to read the user-agent string and redirect anyone who isn't 'me'.

To do this, I use Firefox and it's user-agent string thingy (extension) and setup a UAS that only I would dream up.

At that point you can use your htaccess or a php script to redirect everyone but you.

So, lets say you set up in Firefox a UA of 'MatthewHSE'. Then in PHP you would have a:

if($_SERVER['HTTP_USER_AGENT'] == "MatthewHSE") {
// do nothing
}
else {
// redirect here
location("");
}

I'm pretty sure that I've done the same thing via an htaccess file...



This works great for a quick and dirty solution, and also for live troubleshooting:
if($_SERVER['HTTP_USER_AGENT'] == "MatthewHSE") {error_reporting(E_ALL);}
else error_reporting(0);

MatthewHSE

7:09 pm on Jan 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using the UA string is a great idea; should make it easier to allow bots to have access to the site while the work is being done too. So now I only need to figure out how to do it with .htaccess, and I should be ready to go!

Thanks,

Matthew

MichaelBluejay

4:23 pm on Jan 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think this would work in your .htaccess file:

order allow,deny
allow from 123.45.6.7
deny from all

Substitute your own IP, of course. Let us know if this works.