It’s pretty amazing that even Stack Overflow failed this time when was googling a snippet for redirecting all traffic to a custom maintenance page, except one IP (mine) with Nginx. When finally found some solutions from page three, noticed it were not enough and composed my solution.
In WordPress, there are plugins for maintenance and on any website, you can code your own solutions. However, using hours for just to display short information about ongoing maintenance and restricting access from everyone but you is just overkill.
Inside server block, add the following snippet and edit to your needs. After the maintenance is over, you can just set $maintenance off, and you’re good to go. Create your maintenance page to yourdomain.com/maintenance.
This snippet is for Nginx. If you still use Apache, unfortunately, no luck here.
server {
# Here should be the needed server_name and other settings
# Maintenance mode
set $maintenance on;
if ($remote_addr ~ (176.72.246.31|176.72.246.32)) {
set $maintenance off;
}
if ($uri ~ ^/(index.php/)?(maintenance|someotherword)/(.*)$ ) {
set $maintenance off;
}
if ($maintenance = on) {
return 503;
}
if ($maintenance = off) {
rewrite ^/maintenance(.*)$ https://yourwebsite.com permanent;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /maintenance break;
}
}
I hope this helps! If you find this useful, I’d be delightful if you left a comment below.
Thanks for reading! I need your attention for a moment.
Did your problem got solved? Did you enjoy this post? If so, consider thanking me on Patreon. Doing this is not free and I'd love you buy me a beer or coffee. If you do that, I might be able to help you if you didn't get your problem solved with this blog post. I know my shit around areas like website design, coding, blogging, digital marketing and SEO so if you want to do business with me in other ways let me know.
Davide
It seems you didn’t close the } in the location @maintenance block.
Kind regards
Roni Laukkarinen
You are correct! Poor article editing from me. Thanks for noticing.
Anonymous
I tried this. Make sure that all redirect URLs and paramaters are in place before using this. The permanent redirect cannot be undone otherwise, and I was unfortunate enough to make this mistake.