htaccess Redirect to Maintenance Page


# MAINTENANCE-PAGE REDIRECT  <IfModule mod_rewrite.c>   RewriteEngine on   RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000   RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]   RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]   RewriteRule .* /maintenance.html [R=302,L]  </IfModule>

Code Explanation

  1. The first line is merely a comment to explain the purpose of the code. It is not processed by the server.
  2. The second line enables Apache’s rewrite engine, mod_rewrite. Depending on your server setup, this line may be unnecessary.
  3. The third line checks to see if the request is coming from your computer. If it is, then the redirect does not happen. For this to work, you need to change the numbers in this line to match your own IP address.
  4. The fourth line prevents an infinite-loop scenario by testing the request against the name of your maintenance page. Obviously, we don’t want to redirect requests for the page to which we are redirecting.
  5. The fifth and final line contains the action. It basically redirects all requests that meet both of the previous rewrite conditions to the specified maintenance page. Apache doesn’t allow us to use 500-level response codes, so we are stuck with the next best thing, a 302 – temporary – response.

Update: Multiple IP Addresses

# MAINTENANCE-PAGE REDIRECT  <IfModule mod_rewrite.c>   RewriteEngine on   RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000   RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000   RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000   RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]   RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]   RewriteRule .* /maintenance.html [R=302,L]  </IfModule>

Post a Comment

0 Comments

Close Menu