Redirecting with htaccess
January 3, 2007 9:33 AM   Subscribe

Taking a site offline with .htaccess. I have a server which is running in production, but I want to take it offline for some maintenance. While I want everyone who calls at any URL to be redirected to index.html, I want admins to be able to view the site in full.

I think something like this in .htaccess:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !xxx.xxx.xxx.xxx
RewriteRule [regexp to rewrite anything.php to index.html]

where xxx.xxx.xxx.xxx is a permitted IP address should work, but it seems not to. Any help? Even if the solution only allows access from localhost (I can run lynx on the server) it'll do fine.
posted by fightorflight to Computers & Internet (6 answers total) 1 user marked this as a favorite
 
Are you sure mod_rewrite is installed?

Personally I'd rename the directory and put up a temporary page in the relevant location, but that's just a kludge, obvy.
posted by loiseau at 9:36 AM on January 3, 2007


Response by poster: Yes, mod_rewrite works fine for the rewriting bit, it's just not letting the correct IPs see the real pages. I don't want to move the pages for various reasons based on earlier kludges :)
posted by fightorflight at 9:41 AM on January 3, 2007


Best answer: Just going to index.html gives you difficulties as it's then difficult to link images in to make it look pretty. My solution, in use in a few places, is to create an /offline/ directory which can then contain index.html and whatever support files (images, css, js) it needs.

Here's my snippet. From the looks, your ! might just need to be a !=. Also note that my solution maps *everything* to the offline page, not just pages that end in php. Since you don't list that rule in full the problem could also exist there.

I've included the second RewriteCond so you can put a number of "admin" IP addresses to permit. Of course, add in your 127.0.0.1 as needed.

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=12.12.12.12
RewriteCond %{REMOTE_ADDR} !=23.23.23.23
RewriteCond %{REQUEST_URI} !^/offline
RewriteRule ^.* /offline/ [R]
posted by devbrain at 9:47 AM on January 3, 2007 [1 favorite]


I've done this a ton of times and I've never used mod rewrite but it has always worked perfectly.

Here's what I do:

1. Create a "down for business" page in a completely different place, in my case I created a www2.mysite.com and put the "we're down right now!" page and images there.

2. Change the order to deny, allow

3. Allow the IPs of the testers

4. Set a 403 error document location of www2.mysite.com

Here's the actual code (replace XX with the IPs you want to allow):

order deny,allow
deny from all
#tester IPs
allow from XXX.XXX.XXX.XXX
allow from XXX.XXX.XXX.XXX
ErrorDocument 403 http://www2.mysite.com/
posted by twiki at 2:07 PM on January 3, 2007


Response by poster: Thanks very much everyone. Devbrain yours worked a treat. I'm sure the error plan would work too, twiki, but I wanted to keep it all on the same box.
posted by fightorflight at 7:50 AM on January 4, 2007


There is a whole section on doing exactly this on the Ultimate htaccess article:


Redirect Everyone but you to alternate page on your server.

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1\.1\.1\.1
RewriteCond %{REQUEST_URI} !/temporary-offline\.html$
RewriteRule .* /temporary-offline.html [R=302,L]

That is cool because it redirects users to the page /temporary-offline.html unless the user is coming from a certain IP address.
posted by mrapache at 6:09 AM on March 14, 2007 [1 favorite]


« Older Essential Indie Songs   |   Renting DVD players for long flights Newer »
This thread is closed to new comments.