zealisreal
zealisreal

Reputation: 505

Passing a query string through redirect for maintenance mode

I am looking for some guidance when working with a maintenance mode system, utilised within a .htaccess file, via mod_rewrite and passing a $_GET along with it.

Passing a $_GET in mod_rewrite is perfectly fine under normal circumstances for me, but in this instance I am experiencing issues..

The code I have for the maintenance system is as follows:

RewriteCond                 %{REMOTE_HOST} !^XX\.XXX\.XXX\.XXX

RewriteCond                 %{REQUEST_URI} !/maintenance.php$ 
RewriteCond                 %{REQUEST_URI} !^/css/.*$
RewriteCond                 %{REQUEST_URI} !^/assets/.*$ 
RewriteRule ^$              maintenance.php [R=302,L]

So what I need is the ability to pass a $_GET along with the rewriteRule, thus when the site is viewed as normal by anyone using the allowed IP, I can define that the site is being viewed in maintenance mode.

Of course people not on the allowed IP get redirected to maintenance.php file and don't need this reminder anyway, as the page does that already.

Thank you in advance anyone that can help me in this issue.

EDIT::

# Start the mod re-write conditions #

RewriteRule ^([^/]+)\/$                         ?cat=generic&page=$1 [L]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$      ?cat=product&page=$2 [L]
RewriteRule ^theteam\/([^/]+)\/$                ?cat=staff&page=$1 [L]

Thats how I deal with the other links on my page, I hope that is all you needed to see.

Dan.

Upvotes: 0

Views: 149

Answers (1)

Prix
Prix

Reputation: 19528

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !maintenance.php
RewriteCond %{REQUEST_URI} !(css|assets).*$
RewriteCond %{REMOTE_ADDR} !^XX\.XXX\.XXX\.XXX$
RewriteRule (.*) /maintenance.php [R=302,L]

RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteRule (.*) $1?mode=maintenance [L,QSA]

This works for me, first you have the negation redirect so whoever tries to access without the allowed IP will go to maintenance.php.

Then you have the append internal redirect, the GET mode=maintenance will not be visible but will be there.

And you can retrieve it with $_GET['mode'].

If you want it to visually append to the query string and only to php files you can use:

RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteCond %{QUERY_STRING} !^mode=maintenance.*$
RewriteRule (.*) $1?mode=maintenance [R,L,QSA]

Update of visible rule for your sub directories format this rule should be placed before it like this:

RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteCond %{QUERY_STRING} !.*mode=maintenance.*$
# for visible query string uncomment the below line and comment the next rule
#RewriteRule (.*) $1?mode=maintenance [R,L,QSA]
# for invisible query string
RewriteRule (.*) $1?mode=maintenance [L,QSA]

# Start the mod re-write conditions #

RewriteRule ^([^/]+)\/$                         ?cat=generic&page=$1 [L,QSA]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$      ?cat=product&page=$2 [L,QSA]
RewriteRule ^theteam\/([^/]+)\/$                ?cat=staff&page=$1 [L,QSA]

Upvotes: 1

Related Questions