Htaccess rewrite to service page if exists, not IP, otherwise bootable

How would I rewrite all requests maintenance.php

if it exists, except for images and except for the IP whitelist.

If it maintenance.php

doesn't exist, it should be overwritten in bootstrap ( index.php

) if the requested file doesn't exist.

If maintenance.php

exists and the IP is whitelisted then it should be overwritten in bootstrap ( index.php

) if the requested file does not exist.

I've tried many options:

<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>

      

+3


source to share


3 answers


I would do it in reverse order. Overwrite to the hopper if the maintenance page does not exist or the IP address 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 on this using propositional logic, but why bother ...

+3


source


This is what I ended up using

I hope this helps 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 check mark because he helped me arrive at this solution.

+3


source


Try to include your IP addresses in 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>

      

+1


source







All Articles