19

I'd like to implement mod_rewrite to put my site into maintenance. Basically all IP addresses except a handful we specify would be forwarded to a static html page.

Please can someone help with this rule. Also is there a way to turn this on and off easily without editing the htaccess file?

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Josh
  • 6,256
  • 2
  • 37
  • 56

5 Answers5

21

You can use the REMOTE_ADDR variable in a RewriteCond

RewriteCond %{REMOTE_ADDR} !^10\.0\.1\.1$
RewriteRule ^ /maintenance.html

Just change the condition to match the IPs you want, for more than one you can use ^(ip1|ip2|...|ipn)$.

About how to disable the maintenance mode without changing the .htaccess file I think that's not possible short of writing a program that would delete it or otherwise modify it, an easy one would be to rename it.

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
8

I'd like to slightly correct Vinko Vrsalovic's answer.

RewriteCond %{REMOTE_ADDR} !^10\.0\.1\.1$
RewriteRule ^ /maintenance.html

This rule result will be infinite loop and HTTP server error, because it will be executed on redirection page too. To make it work you should exclude redirection page from the rule. It can be done this way:

RewriteCond %{REMOTE_ADDR} !^10\.0\.1\.1$
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule .* /maintenance.html [R=302,L]
5

Small improvement to Alexander's answer, it's not necessary to use regular expression for the IP address.

RewriteCond %{REMOTE_ADDR} !=10.0.0.1
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule .* /maintenance.html [R=302,L]
8ctopus
  • 2,617
  • 2
  • 18
  • 25
  • 2
    This isn't doing what you think it's doing. `.` is a wildcard in regex, so your first `RewriteCond` line will work, but depending on the IP address used, it might also unintentionally allow other IPs to slip through. (In other words, [the second parameter to `RewriteCond` is *always* a regex pattern](https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritecond)) – rinogo Jun 11 '20 at 17:11
  • 1
    @rinogo thank you for noting the issue. I've fixed it by adding an equal sign before the ip address. (Treats the CondPattern as a plain string and compares it lexicographically to TestString.) – 8ctopus Jun 15 '20 at 08:17
  • 1
    Cool - didn't know you could do that, but the documentation confirms it! :) – rinogo Jun 15 '20 at 16:24
0

you could enable this state and disable it via some admin interface that is able to write to .htaccess (e.g. permissions set to 755 or 777). it would just always find the .htaccess, insert those two lines at the beginning and on disabling maintenance it would delete those two lines, leaving the rest of the file untouched

dusoft
  • 11,289
  • 5
  • 38
  • 44
0

Optional redirect only specific addresses

Late to the party, and just an add-on if somebody needs it the other way around.
With this approach, you redirect only specific addresses into maintenance then play with the aliases.

ServerName  10.0.1.1
ServerAlias 10.0.2.1
ServerAlias 10.0.3.1

RewriteEngine On
RewriteRule ^(.*)$ http://www.domainname.com/maintenance.html$1 [L,R=301]
leopold
  • 1,971
  • 1
  • 19
  • 22