homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirect to include all pages in a directory

 7:37 pm on Jul 15, 2012 (gmt 0)

I have this in my htaccess file to serve up a custom 503 page on Sundays:

# serve specific content on Sunday (Day 0) - custom error page
RewriteCond %{TIME_WDAY} 0
RewriteRule ^index\.shtml$ /503.shtml

How do I edit the rewrite rule so this applies to all shtml pages on my site, not just the index.shtml page?



 11:02 pm on Jul 15, 2012 (gmt 0)

I saw the thread title and thought "Didn't I answer this identical question just last week?" Yup, I did. Well, apart from the Never On Sunday element. That's a new one.

What have you tried so far?

:: inescapable detour to YouTube before going to look for Why We Ask You To Try It Yourself boilerplate ::


 6:13 am on Jul 16, 2012 (gmt 0)

^index\.shtml$ is the RegEx pattern for matching the requested URL. Alter the pattern.

You'll also need a preceding
RewriteCond to detect that %{REQUEST_URI} is not !^/503\.shtml$ to prevent an infinite loop.

Add the [L] flag to the rule.


 11:51 am on Jul 16, 2012 (gmt 0)

I'm a newbie. To clarify, I want to redirect all of my shtml pages to the 503.shtml custom error page on Sundays. Currently as it is now only the index.shtml page gets redirected.


 4:39 pm on Jul 16, 2012 (gmt 0)

Yup, because you're telling it to. So now you need to tweak your RewriteRule to redirect all other pages as well-- excluding the 503 page itself.

Does the 503 page have to be .shtml? Option B is to save it as fixed text with a plain .html extension, and then you don't have to add a RewriteCond to exclude it.

You can't be a brand spanking newbie; I don't think I've ever used %{TIME_WDAY} in my life and had to go look it up ;)

Oh, and why do you want to redirect to an error page if you're not actually serving up a 503 error? In general you should do one or the other: Either make the error, which automatically leads to the ErrorDocument if it's been declared earlier, or redirect to a named page. What happens if there's a natural 503 error at some other time?


 12:41 am on Jul 17, 2012 (gmt 0)

Yes I can easily change the 503 to .html

The 503 is only a "service unavailable" return. My business service is unavailable only on Sundays, so that is why I want to do this. It's a 'custom' error page.

Seems like it would be fairly easy to direct all pages in the www directory (root level). Any thoughts on the RewriteRule?

And no, I'm not a total newbie but just know enough about htaccess to be dangerous!


 1:16 am on Jul 17, 2012 (gmt 0)

So what's your new code to internally rewrite requests for .shtml URLs to fetch content from the 503 file?

And have you tested that the response code really is 503? You must not redirect these requests, a redirect returns a 301 or 302 response.


 11:48 pm on Jul 17, 2012 (gmt 0)

ErrorDocument 503 "Site is temporarily offline."
RewriteEngine On
RewriteRule .* - [R=503,L]


 1:32 am on Jul 18, 2012 (gmt 0)

All those electrons used to formulate post
#4475966 were completely wasted.

 10:49 am on Jul 18, 2012 (gmt 0)

And finally, this is what I've come up with, which seems to work perfectly:

# serve specific content on Sunday (Day 0) - custom error page
RewriteCond %{TIME_WDAY} 0
RewriteCond %{REQUEST_URI} =/503.html
RewriteRule (.*) $1 [L]
RewriteRule .* - [R=503,L]


 12:32 pm on Jul 18, 2012 (gmt 0)

I'll be darned.
Any valid HTTP response status code may be specified, using the syntax [R=305], with a 302 status code being used by default if none is specified. The status code specified need not necessarily be a redirect (3xx) status code. However, if a status code is outside the redirect range (300-399) then the substitution string is dropped entirely, and rewriting is stopped as if the L were used.

Shouldn't be so surprised; mod_alias (Redirect by that name) works in much the same way. Redirect 410 and so on.

But the whole Rule seems backward. "If it's Sunday (Cond 1) and the request is for the 503 page (Cond 2), then stop here and do nothing more (Rule 1). If either condition is not met, then proceed to the next Rule, which slaps on a 503." What happens if it's not Sunday?

Seems like it ought to be

RewriteCond %{TIME_WDAY} 0
RewriteRule \.shtml$ - [R=503,L]

No mention of the 503 page because it doesn't end in shtml. No fallback Rule, because this one serves up the 503 when needed; otherwise it's business as usual. Technically the L flag is not necessary with a 503, but it does no harm and is a good habit.

You said at the outset that you only needed the rule to apply to shtml pages, because your server takes its Christianity seriously. So there's no need to backstep and evaluate Conditions if the request is for anything else, on any day of the week.


 4:22 pm on Jan 13, 2013 (gmt 0)

I've been using this .htaccess code in my root directory for my website to serve up specific content on a particular day of the week:

# serve specific content on Sunday (Day 0) - custom error page
RewriteCond %{TIME_WDAY} 0
RewriteRule !^503\.shtml$ - [R=503,L]

I have one directory (mydomain/AutoIndex/) that I want to exclude from this rule. How do I edit the code?


 10:44 pm on Jan 13, 2013 (gmt 0)

This is what I've come up with:

# serve specific content on Sunday (Day 0) - custom error page
RewriteCond %{TIME_WDAY} 0
RewriteRule ^(AutoIndex)($|/) - [L]
RewriteRule !^503\.shtml$ - [R=503,L]

Seems to be working great. Would like for someone to check the code.


 12:12 am on Jan 14, 2013 (gmt 0)

I never like having a rule that matches and then does nothing.

I prefer to attach a negative match condition before the second rule

RewriteCond %{REQUEST_URI} !^AutoIndex($|/)

in place of the first rule.


 3:18 am on Jan 14, 2013 (gmt 0)

RewriteCond %{TIME_WDAY} 0
RewriteRule ^(AutoIndex)($|/) - [L]
RewriteRule !^503\.shtml$ - [R=503,L]

Is that a direct cut & paste? It would make more sense to use g1's approach and change Rule 1 into a second Condition and have both of them apply to Rule 2, which now becomes your only rule:

RewriteCond %{TIME_WDAY} 0
RewriteCond %{REQUEST_URI} !^/AutoIndex
RewriteRule !^503\.shtml$ - [R=503,L]

You may not need anchors on AutoIndex at all. But opening anchors tend to save time because the server doesn't have to read the whole request. If it doesn't start with /AutoIndex, that's it. Finished. Closing anchors depend on whether you have, or might later get, directories named /AutoIndexSomething else.

If it's in a Condition rather than a rule, doesn't the Request URI need a leading slash? (I detoured to double-check on an existing rule of my own. This also confirms that the "I'm sorry, but the server thinks you are a robot" page works correctly :))

Reminder: Always put a space before each Rule. Among other reasons, it makes it easier to remember that the Conditions only apply to the immediately following Rule, not the whole package.

Special dispensation to omit space if you've got a group of closely related, conditionless rules.


 5:23 am on Jan 14, 2013 (gmt 0)

Yes, the !^ should have been !^/


 1:17 pm on Jan 14, 2013 (gmt 0)

Thank you for checking/updating the code. It is working great.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved