In my previous article on temporarily redirecting visitors during site updates, I present numerous PHP and HTAccess methods for handling traffic during site maintenance, updates, and other temporary periods of downtime. Each of the PHP methods presented in the article allow for access from a singleIP while redirecting everyone else. In this article, we modify our previous techniques to allow access for multiple IP addresses while temporarily redirecting everyone else to the page of our choice. Plus, while we’re at it, we’ll explore a few additional ways to adapt and use the general technique.

Option One — Quick & Dirty

This is the easiest way to inform your visitors that the site is temporarily unavailable. Especially useful for quick site updates, this code will return a “503 Service Unavailable” HTTP status code to the client device while displaying a customized message informing visitors that a site update is in progress. Simply edit the following code with the allowedIP addresses and place at the very beginning of your header.php file:

Here are the items you should customize for the previous code to function properly:

Edit the value for the “$allow” variable in the first line to include the various IP addresses for which you would like to allow; all other IPs will receive the update message and accompanying status code.

Edit the value of “$retry” variable in the third line to reflect the amount of time that should transpire before the client/visitor should revisit your site.

If desired, edit the temporary “maintenance message” in the sixth line with whatever text, markup, or script you wish.

Option Two — Full Meal Deal

This method is more flexible in that it provides a temporary redirect to an alternate location. This is useful when you would like to send your visitors to a completely customized maintenance page or to a preexisting page on a different domain. As before, this method also delivers a “503 Service Unavailable” HTTP status code to the client and instructs a revisit after the specified period of time. To throw it down, first edit the next code block according to the list below and place at the top of your header.php file:

Here are the items you need to customize for the previous method to function properly:

Edit the value for the “$allow” variable in the first line to include the various IP addresses for which you would like to allow; all other IPs will receive the update message and accompanying status code.

Edit the “("Location: http://..)” in the third line to reflect the location of your customized maintenance page.

Edit the value of “$retry” variable in the third line to reflect the amount of time that should transpire before the client/visitor should revisit your site.

Option Three — Combo Pack

Our third method for redirecting site visitors during updates and maintenance is somewhat of a hybrid of the first two methods. As before, we are redirecting visitors to an alternate location, only this time we are replacing the content of the target maintenance page with an informative message. This option provides yet another degree of flexibility by enabling you to redirect and replace content on the fly. To set it off, edit and drop this first code chunk into the top of your header.php file:

Here are the items you need to customize for the previous method to function properly:

Edit the value for the “$allow” variable in the first line to include the various IP addresses for which you would like to allow; all other IPs will receive the update message and accompanying status code.

Edit the “("Location: http://..)” in the third line to reflect the location of your customized maintenance page.

Edit the value of “$retry” variable in the third line to reflect the amount of time that should transpire before the client/visitor should revisit your site.

If desired, edit the temporary “maintenance message” in the sixth line with whatever text, markup, or script you wish.

Additional Ideas and Implementations

While thinking of different ways to use this PHP redirect technique, the first thing that comes to mind is a PHP variation of this HTAccess method for redirecting select visitors to alternate pages. To do this, we need to modify the second line of the second method as follows:

As before, edit that code and place it into into the header of any page(s) for which you would like to restrict access. Keep in mind, however, that this time the IP addresses listed in the array are being blocked. After editing the IP array, simply change the header location in the third line to whatever alternate URL you wish.

Other ideas for this code involve using the code to blacklist an array of IP addresses from site access. You could also use this code to selectively allow certain IPs to various target pages on your site. The take home message here is that this general technique enables you to redirect, restrict, or allow access in a selective manner for any number of pages or directories throughout your site. If you have alternate methods or ideas for implementation, please share by leaving a comment below. Cheers!

Hi Jeff, I’m using this comment form although my question is off topic but couldn’t find anything related but the “(not) Stupid htaccess tricks”. I gotta say I’ve learned a lot from this site and have it as constant reference, really appreciate all your effort.

Anyway, going to the question: is there a way to limit certain content to a given amount of views and after that it serves a different page? specifically, I want to limit the amount of flv videos people can watch and after that they’ll get served a “max limit of views reached” page. Hope that makes sense since my English isn’t the best

Hi Fabio, that’s a new one for me — haven’t heard of any way to do that with HTAccess, but you should be able to find a way to do it with PHP or some other programming language. There is, however, a way to limit file-sizes with HTAccess, but I don’t think it would serve your purpose.

Jeff,
I have been trying and using this trick over the past couple weeks… I have found that using it when connected wirelessly or even plugged into a router does not work for me… it worked when I plugged directly into a modem but now I don’t have that luxury anymore so I am wondering if there is a trick to this I am missing?

Projects

About the site

Perishable Press is the work of Jeff Starr, professional developer, designer, author, and publisher with over 10 years of experience.
Check out some of Jeff's books and projects, follow on Twitter, or learn more »

Fun fact: Perishable Press has been online since 2005, and features over 800 articles and more than 11,000 comments. More stats »