Apache 重写规则进行维护,htaccess 不允许我的 IP 地址通过

Apache rewrite rules for maintenance, htaccess not allowing my IP address through

我正在制作用于网站维护的模板。花了一段时间才弄清楚 Apache 需要绝对路径,但现在一切正常,缺少 IP。

我试图在激活维护时允许我的 IP 和服务器(域)的 IP,因此维护页面将只提供给访客。但是,我也收到了维护页面。我不擅长正则表达式,所以这可能是一个简单的错误。

这是我的 Apache 指令。

## Maintenance
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # local ip
    RewriteCond %{REMOTE_ADDR} !^111\.111\.33\.44
    # server ip
    RewriteCond %{REMOTE_ADDR} !^222\.222\.333\.444
    # maintenance folder
    RewriteCond %{REQUEST_URI} ^/maintenance/
    RewriteRule .+ - [L]
    # maintenance files
    RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|woff|woff2|eot|ttf|svg) [NC]
    RewriteCond %{REQUEST_URI} !^/maintenance/maintenance\.html$
    RewriteRule ^(.*) https://example.com/maintenance/maintenance.html [R=307,L]
</IfModule>

ErrorDocument 503 /maintenance/maintenance.html
ErrorDocument 307 /maintenance/maintenance.html

<IfModule mod_headers.c>
    #do not cache
    Header Set Cache-Control "max-age=0, no-store"
</IfModule>
## End Maintenance

# Force HTTPS
RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

我设法解决了这个问题,所以万一其他像我一样不擅长正则表达式的人遇到这个问题,它可能会对他们有所帮助。

我有两行不需要的指令,^ 是不必要的或导致问题。 $ IP 后是为了确保类似的更长的IP 无法通过。我也切换到 302 错误,因为它是维护的临时重定向。

确保您的文件夹和 html 文件路径设置正确并使用绝对路径。如果您不使用文件夹,直接指向 html 的路径应该有效。我使用了一个文件夹,因为我还加载了图像和字体并且希望保持整洁。

<IfModule mod_rewrite.c>
    RewriteEngine On
    # local ip
    RewriteCond %{REMOTE_HOST} !^111\.111\.22\.33$
    # server ip
    RewriteCond %{REMOTE_ADDR} !^222\.222\.333\.444$
    # maintenance folder
    RewriteCond %{REQUEST_URI} !^/maintenance/(.)*$
    # maintenance files
    RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|woff|woff2|eot|ttf|svg) [NC]
    RewriteCond %{REQUEST_URI} !/maintenance/maintenance\.html$ [NC]
    RewriteRule .* /maintenance/maintenance.html [R=302,L]
</IfModule>

<IfModule mod_headers.c>
    #do not cache
    Header Set Cache-Control "max-age=0, no-store"
</IfModule>