homepage Welcome to WebmasterWorld Guest from 54.204.249.184
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

    
Rewritecond not working unless using !
problem with RewriteCond %{REQUEST_URI}
Boywonder




msg:4558402
 7:23 pm on Mar 25, 2013 (gmt 0)

I'm having an issue when trying to redirect pages based on their context root. I need to have a maintenance page come up based on a specific context root. (ex. www.builder.com/context1 & www.builder.com/context2 get redirected the the maintenance page but all other contexts don't get rewritten and vice versa) The problem I'm having is, if I use a (! not) to specify not this context root it works fine, but I can't specify the context to be redirected. Any help would be greatly appreciated.


Works
AliasMatch ^/maintenance/(.*) /opt/IBM/HTTPServer/htdocs/maintenance/$1
RewriteCond %{REQUEST_URI} !^/maintenance [OR]
RewriteCond %{REQUEST_URI} !^/context1/* [OR]
RewriteCond %{REQUEST_URI} !^/context2/*
RewriteRule ^/(.*) /maintenance/maintenance-tablets [L,R]

Does not work.
AliasMatch ^/maintenance/(.*) /opt/IBM/HTTPServer/htdocs/maintenance/$1
RewriteCond %{REQUEST_URI} !^/maintenance [OR]
RewriteCond %{REQUEST_URI} ^/context1/* [OR]
RewriteCond %{REQUEST_URI} ^/context2/*
RewriteRule ^/(.*) /maintenance/maintenance-tablets [L,R]

 

g1smd




msg:4558426
 9:05 pm on Mar 25, 2013 (gmt 0)

You should not be issuing a 302 redirect to the maintenance page. You should employ a rewrite (not a redirect) so that the page is returned at the originally requested URL.

Is this code in htaccess or elsewhere? It makes a difference as to the RegEx pattern you use in the RewriteRule to match the request.

/* matches "zero or more slashes". That's not what you want.

lucy24




msg:4558437
 9:58 pm on Mar 25, 2013 (gmt 0)

Anything in the form
!blahblah [OR]
is just asking for trouble.

What you've got here is a textbook case of material that's in a condition when it should be in the body of the rule.

First post:
www.builder.com/context1 & www.builder.com/context2 get redirected the the maintenance page but all other contexts don't get rewritten and vice versa

Unfortunately I have no idea what "vice versa" means here. More worryingly is that you say "redirect" and "rewrite" in rapid succession. I kinda think you mean redirect.

What's wrong with

RewriteRule ^(context1|context2) http://www.example.com/maintenance/maintenance-tablets [L]


? That's assuming you really do want a 302 temporary redirect. And assuming "context1" and "context2" are entirely different strings; otherwise you would of course say "context[12]". The R flag becomes optional as soon as you add the full protocol-plus-domain, which you should always do with any redirect. 302 is the default.

But really, shouldn't you instead have something like

RewriteRule ^(context1|context2) - [R=503,L]

combined with a nice custom 503 page? Don't be fooled by "R=503". That's just mod_rewrite's way of handling any status code that doesn't have a shorthand label like [F] or [G].

Boywonder




msg:4558602
 2:46 pm on Mar 26, 2013 (gmt 0)

First thank you for your help and sorry for my wording, but yes redirect.

In response to the question about the context roots, yes they are completely different, (ex. iOS, android, blackberry, etc...). The issue was they wanted to use a regular html maintenance page for their regular desktop web browsers users, but wanted to supply a json file to the tablet/smartphone users. The systems are independent from one another ( that is why I said vice versa, Android, iOS might be down for maintenance but not the blackberry or web and sometimes just the web might be down but nothing else).

The reason behind not using the rule below, was just to conform to what the previous admin had left behind (sort of standardising). That format ( RewriteCond %{REQUEST_URI} ) was used when they created their maintenance page, but the rule you gave is pretty much used every where else.

I know your rule will work, I just have to make sure I know all possible contexts that they are using.

RewriteRule ^(context1|context2) http://www.example.com/maintenance/maintenance-tablets [L]

phranque




msg:4558617
 3:10 pm on Mar 26, 2013 (gmt 0)

welcome to WebmasterWorld, Boywonder!

without seeing your referenced thread i would guess the reason for using REQUEST_URI in the RewriteCond is to avoid problems with the subsequent access to the maintenance page.

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