Forum Moderators: phranque

Message Too Old, No Replies

Apache maintenance setup using Rewrite

         

Kiran2526

5:58 pm on Jul 7, 2016 (gmt 0)

5+ Year Member



I have setup maintenance.html on Apache using below condition/rule.The maintenance.html file has a banner image, I have given the path of image in the html file.

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REQUEST_URI} !maintenance.html
RewriteCond %{REQUEST_URI} !banner.jpg
RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html

When I hit url http://hostname:port/identity it works fine and the banner image loads but
when I add "/" at the end i.e
http://hostname:port/identity/
the banner image does not load. When ever there is / in the incoming request the banner image is not loading

[edited by: phranque at 12:25 am (utc) on Jul 8, 2016]
[edit reason] unlinked url pattern for clarity [/edit]

whitespace

6:18 pm on Jul 7, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



the banner image does not load.


It would be useful to state what actually happens when the image is requested - how does the server respond?

My guess is that you are getting a 404 because you are using a relative URL to "banner.jpg" in your HTML?

RewriteRule ^.*$ /maintenance.html [R=503,L]


Aside: You don't need to specify a substitution string here (use a hyphen instead) - it is ignored because you are using a status code outside of the 3xx range (the relevant ErrorDocument is triggered for the status code specified). The RewriteRule pattern (^.*$) could also be simplified, since you just need to match everything, for example:


RewriteRule ^ - [R=503,L]


Also, you should really escape the dots in your regex, in order to match a "dot" and not any character. For example:


RewriteCond %{REQUEST_URI} !maintenance\.html
RewriteCond %{REQUEST_URI} !banner\.jpg

Kiran2526

6:44 pm on Jul 7, 2016 (gmt 0)

5+ Year Member



Appreciate your quick reply I will try to update my rule as per your suggestion (RewriteRule ^ - [R=503,L] ) and test.

Below is the rewrite output for two scenarios
1) /identity ( working scenario )
2) /identity/ ( non-working scenario )

1) For http://host:port/identity
( there is no forward slash after identity here and it works fine and image also loads, below is the output I get )


(2) init rewrite engine with requested uri /identity
(3) applying pattern '^.*$' to uri '/identity'
(4) RewriteCond: input='/path/htdocs/maintenance.html' pattern='-f' => matched
(4) RewriteCond: input='/identity' pattern='!maintenance.html' => matched
(4) RewriteCond: input='/identity' pattern='!banner.jpg' => matched
(2) forcing responsecode 503 for /identity
(2) init rewrite engine with requested uri /maintenance.html
(3) applying pattern '^.*$' to uri '/maintenance.html'
(4) RewriteCond: input='/path/htdocs/maintenance.html' pattern='-f' => matched
(4) RewriteCond: input='/maintenance.html' pattern='!maintenance.html' => not-matched
(1) pass through /maintenance.html
(2) init rewrite engine with requested uri /banner.jpg
(3) applying pattern '^.*$' to uri '/banner.jpg'
(4) RewriteCond: input='/path/htdocs/maintenance.html' pattern='-f' => matched
(4) RewriteCond: input='/bbt_banner.jpg' pattern='!maintenance.html' => matched
(4) RewriteCond: input='/bbt_banner.jpg' pattern='!banner.jpg' => not-matched
(1) pass through /banner.jpg


2) This is the non working scenario where page images does not load I am using http://host:port/identity/

(2) init rewrite engine with requested uri /identity/
(3) applying pattern '^.*$' to uri '/identity/'
(4) RewriteCond: input='/path/htdocs/maintenance.html' pattern='-f' => matched
(4) RewriteCond: input='/identity/' pattern='!maintenance.html' => matched
(4) RewriteCond: input='/identity/' pattern='!banner.jpg' => matched
(2) forcing responsecode 503 for /identity/
(2) init rewrite engine with requested uri /maintenance.html
(3) applying pattern '^.*$' to uri '/maintenance.html'
(4) RewriteCond: input='/path/htdocs/maintenance.html' pattern='-f' => matched
(4) RewriteCond: input='/maintenance.html' pattern='!maintenance.html' => not-matched
(1) pass through /maintenance.html
(2) init rewrite engine with requested uri /identity/banner.jpg
(3) applying pattern '^.*$' to uri '/identity/banner.jpg'
(4) RewriteCond: input='/path/htdocs/maintenance.html' pattern='-f' => matched
(4) RewriteCond: input='/identity/banner.jpg' pattern='!maintenance.html' => matched
(4) RewriteCond: input='/identity/banner.jpg' pattern='!banner.jpg' => not-matched
(1) pass through /identity/banner.jpg

Note: As you can see in secound case its looking for banner.jpg under /identity but there is no image there so the image is not loading , the image is present under same path were maintenance.html is.

[edited by: phranque at 12:24 am (utc) on Jul 8, 2016]
[edit reason] unlinked url pattern for clarity [/edit]

whitespace

10:34 pm on Jul 7, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



its looking for banner.jpg under /identity but there is no image there so the image is not loading


My guess is ... because you are using a relative URL to "banner.jpg" in your HTML?


It would seem that you are using a relative URL to reference your image - that is the problem. The fix is to either use a root-relative - starting with a slash - (or absolute) image URL (ie "/banner.jpg") or include a BASE element in the HEAD section, stating the URL that all relative URLs are relative to.

This problem should not be fixed with .htaccess. It is an HTML / client-side issue.

lucy24

4:37 am on Jul 8, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is not an Apache problem. It is, as whitespace says

:: pause to wave arms happily because he's been gone for ages ::

purely an HTML problem. The same thing would happen in any error document where the visitor's browser "thinks" they are at suchandsuch URL. (Errors are always served at the originally requested URL.) So if the error document contains relative links, the browser will parse those relative links based on where it "thinks" it is.

Solution: never use relative links in error documents. (Also don't use them in any rewrite where you end up in a different directory than the one originally requested.)

Kiran2526

3:08 pm on Jul 14, 2016 (gmt 0)

5+ Year Member



Many thanks Whitespace and Lucy. Your inputs have helped me to resolved the issue. I changed the image path to "/banner.jpg" and it started working perfectly. Also I update the rule as below

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REQUEST_URI} !maintenance\.html
RewriteCond %{REQUEST_URI} !bbt_banner\.jpg
RewriteRule ^ - [R=503,L]
ErrorDocument 503 /maintenance.html

Once again many thanks for taking ur time and helping me out