Ever want to redirect your site visitors to a temporary maintenace page while you’re doing some maintenance behind the scene, but you don’t know how?.
The answer to that problem is simple, as long as you have access to edit your .htaccess file (some web hosting company doesn’t allow you to edit the .htaccess file, and that’s why i tell you beforehand) :)
And here are the complete instruction :
1. Create a new html page saying that this site under maintenance or anything you like :) , and save it as maintenance.html (this is just an example, feel free to use another filename)
2. Edit your .htaccess file in your root public_html directory
and add these lines :
RewriteCond %{REMOTE_ADDR} !^123.456.789.0 RewriteCond %{REQUEST_URI} !(maintenance.html|image.jpg|and-other-files-you-want-to-add)$ [NC] RewriteRule .* /maintenance.html [R,L]
Note: Replace 123.456.789.0 with your own ip address
And here is the description about what the above line does :
- The first RewriteCond is used so that you can still access your site, by detecting your ip address
- The second RewriteCond is used so that every request to maintenance.html file will not be redirected, this is used to prevent infinite redirect loop
- The third RewriteCond is used so that others that is trying to access your site in any page will be redirected automatically to your maintenance.html page using temporary redirect so that search engine robot will check again later
But what if you want something more creative like faking the extension to make it looks like that you’re using ASP? then here’s some more example
RewriteRule ^maintenance.asp maintenance.html [NC,L] RewriteCond %{REMOTE_ADDR} !^123.456.789.0 RewriteCond %{REQUEST_URI} !(maintenance.html|maintenance.asp|image.jpg|and-other-files-you-want-to-add)$ [NC] RewriteRule .* /maintenance.asp [R,L]
And what if you’re finished with the maintenance?
It’s simple you just need to remove the above .htaccess rules or better yet, to make things easier in the future, just put # in front of each lines, and to make sure that your site visitors will be automatically redirected to your main page, you can add below line to your .htaccess file like below for example
#RewriteCond %{REMOTE_ADDR} !^123.456.789.0 #RewriteCond %{REQUEST_URI} !(maintenance.html|image.jpg|and-other-files-you-want-to-add)$ [NC] #RewriteRule .* /maintenance.html [R,L] Redirect 301 /maintenance.html http://www.yourdomain.com
And you’re done :)
When I do this it won’t display the JPEG in the html file I’m pointing it to. The image displays fine when I access the file via mydomain.com/maintenance.html, what the hell is going on??!
I’m sorry, i guess i forgot to mention that you need to add the image filename (or other filename you want to add into the .htaccess directives) so they can be displayed. I’ve updated the code example in this post
great! this is what im lookin for thanks… jangan pernah putus berbagi ya!! ;)
Thanks for this!
Perfect – thank you. I had to figure this out in the absence of my developer today…and you spelled it out nicely, even for non-tech-types like myself. Thanks again.
I do love the classics.