Author |
|
jack01
Joined: 28 Feb 2014 Posts: 27
|
Posted: Fri 16 Dec '16 8:52 Post subject: How to display web info page during maintainance? |
|
|
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: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Fri 16 Dec '16 12:24 Post subject: |
|
|
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). |
|
Back to top |
|
jack01
Joined: 28 Feb 2014 Posts: 27
|
Posted: Fri 16 Dec '16 15:18 Post subject: |
|
|
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:
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: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Mon 19 Dec '16 12:25 Post subject: |
|
|
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
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
|
Posted: Tue 20 Dec '16 12:05 Post subject: |
|
|
@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
|
Posted: Tue 20 Dec '16 12:24 Post subject: |
|
|
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: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Tue 20 Dec '16 12:47 Post subject: |
|
|
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
|
Posted: Tue 20 Dec '16 13:06 Post subject: |
|
|
@mraddi, I have overlooked the obvious. Very elegant solution. Thanks it solves the problem. |
|
Back to top |
|
jack01
Joined: 28 Feb 2014 Posts: 27
|
Posted: Tue 20 Dec '16 13:17 Post subject: |
|
|
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
|
Posted: Tue 20 Dec '16 13:20 Post subject: |
|
|
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 |
|