How would I rewrite all requests to maintenance.php
, if it exists, except images, and except an IP white list.
If maintenance.php
does not exist it should rewrite to a bootstrap ( index.php
) if the requested file does not exist.
If maintenance.php
does exist and the IP is in the white list, then it should rewrite to a bootstrap ( index.php
) if the requested file does not exist.
I have tried many variations of the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/maintenance.php -f
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif)$
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123$
RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111$
RewriteRule . maintenance.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
I would do it in reverse. Rewrite to the bootstrap if the maintenance page doesn't exist or the IP is valid.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/maintenance.php !-f [OR]
RewriteCond %{REMOTE_ADDR} ^123\.123\.123\.123$ [OR]
RewriteCond %{REMOTE_ADDR} ^111\.111\.111\.111$
RewriteRule . index.php [L]
RewriteCond %{DOCUMENT_ROOT}/maintenance.php -f
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif)$
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123$ [OR]
RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111$
RewriteRule . maintenance.php [L]
So, basically it will run index.php IF:
(Not a file)
&& (Not a dir)
&& (
(Maintenance Doesn't Exist)
|| (Remote Addr == 123.123.123.123)
|| (Remote Addr == 111.111.111)
)
We could expand this out using propositional logic, but why bother...
This is what I ended up using
I hope it help any future visitors:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/maintenance.php !-f [OR]
RewriteCond %{HTTP:X-Forwarded-For} ^(x\.x\.x\.x|y\.y\.y\.y|z\.z\.z\.z)$ [OR]
RewriteRule . index.php [L]
RewriteCond %{DOCUMENT_ROOT}/maintenance.php -f
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif)$
RewriteCond %{HTTP:X-Forwarded-For} !^(x\.x\.x\.x|y\.y\.y\.y|z\.z\.z\.z)$
RewriteRule . maintenance.php [L]
</IfModule>
Also @ircmaxell gets the tick because he helped me come to this solution.
Try enclosing your IP addresses within brackets in the same RewriteCond
:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/maintenance.php -f
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif)$
RewriteCond %{REMOTE_ADDR} !^(123\.123\.123\.123|111\.111\.111\.111)$
RewriteRule . maintenance.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.