3

I have the following 503 status set:

RedirectMatch 503 ^/(?!index\.html)
ErrorDocument 503 /index.html
Header always set Retry-After "604800"

I understand that this rule tells any robots that the server is down for maintenance and to check back in a week.

This is working perfectly, but the server is failing to load any images or CSS. I've checked via the Google Chrome console and receive the following error:

Failed to load resource: the server responded with a status of 503 (Service Temporarily Unavailable)

How do I add to the .htaccess file to include CSS and image files (JPG, PNG etc)?

In addition to this question, can you recommend a good resource for learning about .htaccess? The Apache documentation seems to be very bloated and difficult to navigate.

2 Answers 2

2

Have mod_rewrite installed and try this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^(.*).png [NC]
RewriteCond %{REQUEST_URI} !^(.*).jpg [NC]
RewriteCond %{REQUEST_URI} !/index.html [NC]
RewriteCond %{REQUEST_URI} !^(.*).css [NC]
RewriteRule ^(.*)$ - [R=503,L]
ErrorDocument 503 /index.html
Header always set Retry-After "604800"

The 4th line checks the request to see if the part of the URL coming after the domain name is /index.html. The 2nd line checks to see if the URL ends in .gif. The 3rd line checks to see if it ends in .jpg, and the 5th line checks to see if it ends in .css. Add another line below it if you need to enable another file extension. The 6th line means match anything when all the criteria is true (meaning all the rewritecond directives return true) and redirect it to error 503.

The 7th line sets the error 503 page to the contents of the /index.html page. The last line adds HTTP headers that cause the browser to store the copy of the page for 604800 seconds. This should be removed (and better yet, replaced with a no-caching option) if you plan to fix your site sooner than 604800 seconds.

8
  • I've checked with my host and mod_rewrite is on. With your method, I'm getting the console error GET domain.com/style.css 503 (Service Temporarily Unavailable).
    – Sam
    Commented Jul 9, 2015 at 9:03
  • oh shoot. I should have included a (.*) between ^ and . for every rewritecond line except for the line containing index.html to specifically force a "match anything" section before the file extension. and is console error part of the contents of index.html? What exactly do you want your 503 error message to be? because index.html normally is a file to represent a home page, not an error page. Commented Jul 9, 2015 at 16:00
  • That did the trick, thank you! I've updated your answer to include the missing bits. I've changed index.html to holding.html. I originally named it index.html because there was nothing else in the directory. One final thing before I mark the question as answered... I checked your code via this validator. I'm getting the following warning at line 6: This rule was met, the new url is domain.com/- Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 503. Anything to be concerned about?
    – Sam
    Commented Jul 9, 2015 at 17:19
  • R means redirect. R=503 is normal in your case. If you really wanted to play around, you could easily change it to an access denied error page by changing R=503 to R=403 because 403 means forbidden (access denied). Commented Jul 9, 2015 at 17:31
  • 1
    .htaccess is basically processed as the dynamic section of apache's httpd.conf file where certain settings can apply immediately once changed without restarting the server. You may want to research tutorials on mod_rewrite to see how you can remap a whole host of URLs to other URLs, etc. Commented Jul 9, 2015 at 17:48
0

As you have done with your current rules, you need to make an allowance for any files that end in .jpg or .png. So for example, I think your redirect match would be:

RedirectMatch 503 ^/(\.(html|png|jpg|jpeg|gif)|~)$)

This has the added benefit that it will catch any html file that's being called during maintenance mode. I haven't tested the above, but basically you need to match the file extensions being called.

3
  • Would I create a new line with the RedirectMatch 503 you've provided or replace the current RedirectMatch 503 with your version?
    – Sam
    Commented Jul 8, 2015 at 10:30
  • I would try to replace the one you have. Commented Jul 8, 2015 at 10:30
  • I'm getting an Internal Server Error (500) by replacing that line.
    – Sam
    Commented Jul 8, 2015 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.