How to Redirect Visitors to Maintenance Page Using .htaccess

Sometimes, you may want to put your site into maintenance mode so that you can do required up-dates like modifying themes, including custom features, repairing corrupted files.

In this article you will find how to redirect all traffic and all visitors of your site to a maintenance page during site updates.

Create a Simple Maintenance Page

You first need to create a simple maintenance page with HTML into your root directory.

Create a new file named maintenance.html

Now open the newly created file and place the below code in it.

<!doctype html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>Under Maintenance - Check Back Later</title>

  <meta name="description" content="Under Maintenance - Check Back Later">

  <meta name="author" content="Vamsi">

  <link href='//fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>


  <style>

     html {

      background: #f1f1f1;

     }


     body {

      background: #fff;

      max-width: 70%;

      font-family: "Open Sans", sans-serif;

      font-size: 14px;

      padding: 1.5em 2em;

      margin: 5em auto;

      -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);

      box-shadow: 0 1px 3px rgba(0,0,0,0.13);

     }

  </style>

  <!--[if lt IE 9]>

  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

  <![endif]-->

</head>

<body>

  <h1>Under Maintenance - Check Back Later</h1>

  <p>We are performing some updates and maintenance tasks.</p>

  <p>Please check back later.</>


</body>

</html>

Once done save and close the file.

After uploading the file, you would be able to access the HTML file using the URL http://yourdomain.com/maintenance.html.

Add .htaccess Rule

Now, open the .htaccess file in the root directory, copy the below code and paste it in the file. Save it and upload the file again.

# Redirect Visitors to Maintenance Page

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REMOTE_ADDR} !^192\.168\.100\.100

RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]

RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]

RewriteRule .* /maintenance.html [R=302,L]

</IfModule>

Make sure to change the IP address in line 4 to match your current IP address.

Now all your site visitors will be redirected to the maintenance page. But you can still access your site as we’ve white listed your current IP address.

If you want to take away the maintenance mode, just remove the code you’ve added to the .htaccess file and you are good to go.

That’s all.

 

Leave a Reply