Asked  2 Years ago    Answers:  5   Viewed   55 times

I have built a website (PHP) with more than 60 pages. I have only now realized (unfortunately) that I should have built in an "In Maintenance Mode" feature to allow an admin to temporarily disable the website and point it to a Maintenance Mode page. This would only allow those logged in as an admin to view the website.

The options I see are:

  1. Add a new "include" file to the top of every single PHP page.

  2. I have one include that is used to display the navigation bar on
    every page (navigation class). I could write the Maintenance Mode
    code in this class.

Do I have any other options? The 1st option doesn't seem like the most efficient, and the 2nd one just seems like bad programming. Is there any other better way to include a new file on every single php file?

Thanks!

ps - the site is not launched yet

 Answers

5

You can use .htaccess to redirect to another page while on Maintenance Mode.

Three assorted examples:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^11.111.111.111
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]

.htaccess “Down For Maintenance” Page Redirect

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$ 
RewriteCond %{REMOTE_HOST} !^888.888.888.888

RewriteRule $ /maintenance.html [R=302,L] 

Redirect to maintenance page during upgrade using .htaccess

# redirect all visitors to alternate site but retain full access for you
ErrorDocument 403 http://www.alternate-site.com
Order deny,allow
Deny from all
Allow from 99.88.77.66

Maintenance mode for apache

Tuesday, August 16, 2022
 
bicycle
 
1

Use THE_REQUEST instead of REQUEST_URI which might change due to other modules or rules:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !s/+preview [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} s/+preview [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

# other rules go here

Also make sure to clear your browser cache.

Tuesday, August 30, 2022
 
ian_m
 
3

My solution, that I haven't seen else where, relies on the use of the RewriteValve, so it's dedicated to SO users. You will need a Tomcat restart to activate it.

Add the valve in your Host definition in $CATALINA_BASE/conf/server.xml :

<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />

Edit or (more likely) create $CATALINA_BASE/conf/Catalina/localhost/rewrite.config file with this content :

# for a missing webapp which would be named fewbars
RewriteCond %{REQUEST_PATH} ^/fewbars/.*
RewriteRule ^(.*)$ /maintenance/index.html [R,NC]

# for a deployed webapp named fewbars
RewriteCond %{CONTEXT_PATH} /fewbars
RewriteRule ^(.*)$ /maintenance/index.html [R,NC]

As commented, the behaviour depends on the webapp status : if the war file and the directory have been removed then you need to match on the REQUEST_PATH. On the contrary, to hide an application whose war and directories are still there it will be matched by CONTEXT_PATH.

You understand that you need a /maintenance/index.html file in $CATALINA_HOME/webapps directory

Tuesday, November 29, 2022
 
oghaza
 
2

I find the solution:

RewriteCond %{REQUEST_URI} Bolivia 
RewriteRule ^ http://www.example.com/US/? [L,R=301]
Saturday, October 29, 2022
 
2

i find out an blog that contains answer for your question please check this site

The below examples are taken from the above mentoined site.

If you don’t already have an .htaccess file in the root of your domain, create one and add the following code. If you already have an .htaccess file, add the following code in front of everything that might be in there

RewriteEngine On

# Add all the IP addresses of people that are helping in development
# and need to be able to get past the maintenance mode.
# One might call this the 'allow people list'
RewriteCond %{REMOTE_HOST} !^83.101.79.62
RewriteCond %{REMOTE_HOST} !^91.181.207.191

# Make sure the maintenance mode only applies to this domain
# Example: I am hosting different sites on my server
# which could be affected by these rules.
RewriteCond %{HTTP_HOST} ^nocreativity.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.nocreativity.com$

# This is the 'ignore file list'. It allows access to all
# files that are needed to display the maintenance mode page.
# Example: pages, css files, js files, images, anything.
# IMPORTANT: If you don't do this properly, visitors will end up with
# endless redirect loops in their browser.
RewriteCond %{REQUEST_URI} !/offline.htm$
RewriteCond %{REQUEST_URI} !/css/style.css$
RewriteCond %{REQUEST_URI} !/images/logo.png$

# Rewrite whatever request is coming in to the maintenance mode page
# The R=302 tells browsers (and search engines) that this
# redirect is only temporarily.
# L stops any other rules below this from executing whenever somebody is redirected.
RewriteRule .*$ /offline.htm [R=302,L]
Saturday, November 12, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages