With any hosting account, there is always a chance of something going wrong and keeping your files from displaying properly. Perhaps your site was hacked and you need some time to restore or clean compromised files. Or maybe you need to take your site down to perform some extended maintenance. In either case, you want to let the customers know you will be back. Just as important, you want to let the search engines know that you are down temporarily and will return. This way they will neither penalize your site nor remove your pages from their index. Here we discuss the proper way to let everyone know using a 503 setting for your cPanel account.
NOTE: This will not work if there is no communication with the server itself. This is for instances when the server is up and running but there is something wrong with the account or files.
A Note About HTTP Codes
Whenever you visit a web page, your browser receives a code from the server. These codes are known as HTTP status codes. Below are some of the most important and common ones.
200 – The HTTP request was successful. This is the expected response.
301 Moved Permanently – This means the requested file has moved to another permanent location. All future requests for this resource should be directed to the new location.
302 Found – The requested file or resource has moved temporarily. This code tells the search engines that the redirect is temporary, as opposed to the 301 redirect. This code will not pass any link juice.
404 Not Found – Most of us are familiar with this code. A 404 error code means the requested item cannot be found at the location and no redirect was given. This code is not known to be either permanent or temporary. Eventually, the file will be removed from the search engine indexes.
503 Service Unavailable – This code means the server is currently unavailable. This could be due to a server overload, in which the server will give out the 503, or the site is under maintenance. Search engines that receive this code know that the situation is temporary. This is the code you should use when your site is under maintenance or undergoing extensive work and needs to be offline.
Advising the Search Engines of your Temporary Situation
If a situation occurs where you need to take the site down temporarily, you will want to let Google and other search engines know. To do this, you would use the 503 status code you read about above. There are a couple of steps to take to set this up.
Create a 503.php file
First, you will want to create a file named “503.php”. This file can serve as a notification for both the servers as well as your human visitors. For the search engines, you will need to place some code at the top of the file. You can copy the code below:
<?php header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Status: 503 Service Temporarily Unavailable"); header("Retry-After: 3600"); ?>
The first two lines define the 503 code. The third line defines when the site is expected to be back. In this case, the number is given in seconds. 3600 seconds is one hour. In some cases, you may want to give a specific date. In that case, you would use the following line instead:
header("Retry-After: Fri, 27 May 2016 12:00:00 GMT");
This line must be given in Greenwich Mean Time (GMT) so you may want to adjust it for your specific time zone.
Once you set the code to let the search engines know about your situation, you can add code to let your visitors know. This can be written with simple HTML such as the following:
<?php header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Status: 503 Service Temporarily Unavailable"); header("Retry-After: 3600"); ?> <!DOCTYPE html> <html> <head> <title>Site is temporarily unavailable due to maintenance</title> </head> <body> <h1>Site is temporarily unavailable due to maintenance</h1> <p>We expect to have the site back up within 4 hours.</p> </body> </html>
Set the redirect in .htaccess
Now that the php file has been created, you will need to set the redirect within your .htaccess file. This way any requests to the site get redirected to it. For those who have some experience creating 301 redirects in the .htaccess file, this will be similar.
- Log into your cPanel.
- Open the .htaccess file for editing using the File Manager.
- Add he following code to the top of the file:
Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{REMOTE_ADDR} !^00\.00\.00\.00 RewriteCond %{REQUEST_URI} !^/503\.php [NC] RewriteRule .* /503.php [L,R]
- Save the file.
If you personally want to have access to the site while everyone else sees the 503, you can edit the fourth line to include your IP address. This way you can see the site and any changes you make normally. So, for instance, if your IP address was 123.123.123.123, you would make the fourth line above look like this:
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
The .htaccess file shown in this article seems to contain an error, which may be a typo. There is no file called 503/php, so the .htaccess file will get into a nasty loop.
The line which is shown as:
RewriteCond %{REQUEST_URI} !^/503/php [NC]
Seems to me that it should be:
RewriteCond %{REQUEST_URI} !^/503\.php [NC]
Thank you for catching that. I updated the article with the proper syntax.
I’m not using any sort of CMS. It’s just php pages running in httpd with php8. A server with php5 has no problems with the 3 header lines. It must be a change in php8 that is causing the problem.
Hello Marc – sorry for any issues you may be facing with PHP 8.0. You are able to change the version of PHP being used in the cPanel. If you continue having problems with it, then I recommend contacting our live technical support team for assistance.
With PHP5 this works, but with PHP8 the body html of the 503.php page does not display, unless you comment the 3 header lines of code. If the header lines are not commented all that displays is this generic message. “Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.”
Hello Marc – Thanks for pointing this out! The article will undergo review and then be updated as time permits. We appreciate that you took the time to let us know!
The html in the body of my maintenance.php page does not display, unless I comment out the 3 header lines.
Hello Marc – Sorry for the issues with the code. It is difficult for us to resolve coding issues due to the variety of causes that could be causing code to not work as expected. I’m not sure if you’re on a WordPress site, or if you’re using a different CMS or a custom-coded web site. It is possible that your HTML is being affected by the template (if you’re using a CMS such as WordPress). As we can’t see your code, I would recommend working with an experienced developer, or submitting the code to a community where they can all take a look and then help you resolve the issue based on their experience.
Thank you so much. You will be surprised at how hard it is to find “Create a 503 Status Code for your Site” when making a Google search. Your page was the first after adjusting my search phrase several times.