大网数据广告活动
网站运营与故障问答

Website is under maintenance - how to restrict access?

· 52CE 我爱测

Currently, my website is under maintenance. I want to be the only person who's able to access/view the website online until I finish maintenance. How to set restrictions to others? I hate stalkers.

问题原文

原帖链接 · Mina Hafzalla · CC BY-SA 3.0

Currently, my website is under maintenance. I want to be the only person who's able to access/view the website online until I finish maintenance. How to set restrictions to others? I hate stalkers.

社区高分解答

原帖链接 · MrWhite · CC BY-SA 4.0

Currently my website is under maintenance.

If your website is only temporarily "under maintenance" and has already been live and indexed by search engines then you should consider returning a "503 Service Unavailable" HTTP response code with perhaps a Retry-After HTTP header indicating when the site is expected to be available again. Instead of simply blocking access to the site with a 401 or 403 response.

The 503 response is specifically for situations when the site is "down for maintenance" and can help lessen the effect any downtime might have on your search engine ranking.

This is easily achieved using mod_rewrite (and mod_headers) in htaccess in order to block IP addresses. However, as mentioned in comments above, blocking IPs (or rather only allowing a select few IPs) can be problematic unless the IPs you are allowing are (reasonably) static or you can allow IP-ranges (if your content isn't too sensitive).

To achieve something like this in htaccess:

# Declare custom error document
ErrorDocument 503 /errordocs/e503.php

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=123.123.123.123
RewriteCond %{REMOTE_ADDR} !=124.124.124.124
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteRule ^ - [R=503,L]

Only the IPs 123.123.123.123 and 124.124.124.124 will be allowed access. Then to send an unconditional Retry-After header:

Header always set Retry-After "Wed, 25 Dec 2013 04:00:00 GMT"

(This could be sent conditionally based on the value of an environment variable for instance.)

However, personally I tend to pass the date/time directly to my error document and allow my script to set the header conditionally and display a meaningful message to the user at the same time (this would also be reqd if you didn't have mod_headers installed):

# Declare custom error document, passing date/time for Retry-After header
ErrorDocument 503 /errordocs/e503.php?RetryAfter=2013-12-25+04:00:00

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2013-11-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。