logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: How to display web info page during maintainance?
Author
jack01



Joined: 28 Feb 2014
Posts: 27

PostPosted: Fri 16 Dec '16 8:52    Post subject: How to display web info page during maintainance? Reply with quote

Hi,
I have Apache httpd 2.4.23 on Windows 2008 R2. System accessed by http server is normally accessed during day. But at night between 2am and 3am system goes down for daily maintenance. Currently at this time web server just returns error, but I would like to display web page with information why and how long system is not going to be available.

Is there a way inside Apache httpd I can schedule some rule to display web page during maintenance hours?

Thanks
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 149
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Fri 16 Dec '16 12:24    Post subject: Reply with quote

Hello,

I've tested this in the .htaccess within the document-root of the apache-webserver.
In this case from 11:11to 11:13 the maintenance-page was served instead of the normal result.
Code:
RewriteCond %{TIME_HOUR}%{TIME_MIN} >1110
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1114
RewriteCond %{REQUEST_URI} !/maintenance/index.html$
RewriteRule ^(.*) http://localhost/maintenance/index.html [P,L]


Other option would be to create a file maintenance_on when maintenance starts, delete it when you are finished and use the following lines wihtin the .htaccess:
Code:
RewriteCond %{DOCUMENT_ROOT}/maintenance/maintenance_on -f
RewriteCond %{REQUEST_URI} !/maintenance/index.html$
RewriteRule ^(.*) http://localhost/maintenance/index.html [P,L]


The exceptions in the RewriteCond for /maintenance/index.html are needed because otherwise the new requests will be redirected to and your apache will run out of threads.
As my /maintenance/index.html does not need other ressources (all CSS, images, JavaScript) are contained within the index.html the exception for /maintenance/index.html is sufficient. You might extend the exception to the complete directory /maintenance/ if you need other ressources from that directory.

I'm sure that there are other/better possibilities available but this is working (at least for me). Very Happy
Back to top
jack01



Joined: 28 Feb 2014
Posts: 27

PostPosted: Fri 16 Dec '16 15:18    Post subject: Reply with quote

mraddi, I am not familiar with .htaccess file (I have never used it).

But I already have a rewrite file, that I am using.
I did the following:
1. Commented out all of the rules except first line:
Code:
RewriteEngine On

2. I copied your fist block rules into it and changed http to https and localhost to %{SERVER_NAME}.
3. Changed the time so current time is inside or outside of the interval.
4. I created maintenance dir with index.html in it.
5. Restarted Apache httpd.

Now if time is inside interval, so maintenance/index.html should be displayed, but it is not I get "403 Forbidden". But if time is outside interval then web pages works normally.

The same behaviour for second block. If file exists then I get "403 Forbidden", if file does not exists then normal web pages works normally.

It looks like something is blocking rewrite. Any idea what should I look into?
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 149
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Mon 19 Dec '16 12:25    Post subject: Reply with quote

Please check if you can access the maintenance-page "by hand" (entering its URL into the browser's address-bar).
Also check the apache's error.log for hints/error-messages.

In addition I simplified the rule a little (removed the hostname and the P for proxifying the request):
Code:
...
RewriteRule ^(.*) /maintenance/index.html [L]


Normally it shouldn't be a real difference (in case of does it work or not) if you configure the Rewrite-rules in your apache-config or in a .htaccess. And from your information I got that the the RewriteCond is working as expeced - only the RewriteRule is making some trouble Confused
The difference (between configuring the Rewrite-Rules in .htaccess compared to do it in apache's config) is that you can modify a .htaccess without restarting apache, you might have to configure something in apache's config to make .htaccess work (AllowOverride...) and it might affect the apache's performance a little as it has to search every directory up to the document-root for a .htaccess.
Back to top
jack01



Joined: 28 Feb 2014
Posts: 27

PostPosted: Tue 20 Dec '16 12:05    Post subject: Reply with quote

@mraddi, I used your new redirect with time rewrite conditional and it is working fine! Thanks.
Back to top
jack01



Joined: 28 Feb 2014
Posts: 27

PostPosted: Tue 20 Dec '16 12:24    Post subject: Reply with quote

Above was working fine when all of existing rules were commented out.

I now turned on my other existing rules and new problem appeared.

I already have rules like:
Code:

<some conditions>
RewriteRule "/some_dir/"  https://%{SERVER_NAME}/info.html [R=301,L]

<your whole code here>


When user accesses the "/some_dir/" I don't what to trigger maintainance web page.

It looks like when "/some_dir/" is accessed then redirect is triggered into info.html and then bellow your code gets triggered and content of maintaince web page is displayed.

Is it possible to say something like: "if you get on "/some_dir/" then redirect to info.html (like now) and stop executing anything bellow this command?

P.S. I know this can be performed using variables (with E flag), but is it something easier.
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 149
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Tue 20 Dec '16 12:47    Post subject: Reply with quote

Within my config there is one line/exception
Code:
RewriteCond %{REQUEST_URI} !/maintenance/index.html$

which means "do this rewrite only if you don't access the /maintenance/index.html. If you are accessing this /maintenance/index.html please continue (...and send its content to the requesting browser)".
So to send the "/info.html" to the browser, you need an exception for this /info.html, too.
Please add the following RewriteCond to "my" maintenance-rewrite-part
Code:
RewriteCond %{REQUEST_URI} !/info.html$

which should result in "do this rewrite to the maintenance-page during the given timeframe and only if it is not already a request for the maintenance-page or a request for the /info.html".
Back to top
jack01



Joined: 28 Feb 2014
Posts: 27

PostPosted: Tue 20 Dec '16 13:06    Post subject: Reply with quote

@mraddi, I have overlooked the obvious. Very elegant solution. Thanks it solves the problem.
Back to top
jack01



Joined: 28 Feb 2014
Posts: 27

PostPosted: Tue 20 Dec '16 13:17    Post subject: Reply with quote

Hi,
another problem appeared. I have figure it out I actually need this maintenance info from 23:30 till 00:30 next day.
It looks like if multiple RewriteCond are used then AND condition is used between them, isn't it? But I would probably need OR condition between time and AND condition for the rest?
Regards
Back to top
jack01



Joined: 28 Feb 2014
Posts: 27

PostPosted: Tue 20 Dec '16 13:20    Post subject: Reply with quote

Hi,
problem solved. According to web page: https://stackoverflow.com/questions/922399/how-to-use-and-or-for-rewritecond-on-apache
OR has a precedence over ordinary AND condition, so my code conditions: (A OR B) AND C AND D looks like:
Code:

RewriteCond %{TIME_HOUR}%{TIME_MIN} >2230 [OR]
RewriteCond %{TIME_HOUR}%{TIME_MIN} <0030
RewriteCond %{REQUEST_URI} !/maintenance/index.html$
RewriteCond %{REQUEST_URI} !/info.html$
RewriteRule ^(.*) /maintenance/index.html [L]

Thanks for help.
Back to top


Reply to topic   Topic: How to display web info page during maintainance? View previous topic :: View next topic
Post new topic   Forum Index -> Apache