Forum Moderators: phranque

Message Too Old, No Replies

wierd rewrite proxy problem

         

deesto

3:39 pm on Oct 21, 2011 (gmt 0)

10+ Year Member



I'm trying to convert some static reverse ProxyPass rules into a rewrite with regex (since ProxyPass doesn't support much regex).

I'd like to convert URL requests for /myurl/somepath/anotherpath/* to be proxied to:
http://www.example.com/dirone/dirtwo/somepath-anotherpath/*

So I try:
RewriteRule ^/myurl/(\w+)/(\w+)/(.*)$ http://www.example.com/dirone/dirtwo/$1-$2/$3 [P]


When I try a URL, I don't get an error, I don't see anything in the apache logs except for a rewrite proxy pass to the proper back-end, but in the client, I get an error, and it's trying to connect to the back-end but can't:

Server not found
Firefox can't find the server at www.example.com.

The target URL on the destination host is definitely reachable via the proxy host (tested via wget). Am I screwing up something in the regex?

deesto

3:02 pm on Oct 26, 2011 (gmt 0)

10+ Year Member



This turned out not to be a regex problem at all (that part was actually correct), but the dreaded 'trailing slash' problem. Once I added another rewrite before this one to turn a directory request not ending with `/` to include the `/`, this worked fine ... unless there's a better way to incorporate the trailing slash fix into the original rule?

lucy24

8:29 pm on Oct 26, 2011 (gmt 0)

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



Are your slashless requests the ones in the form

^/myurl/(\w+)/(\w+)

where the #3 slot (.*) is empty?

Ordinarily the slash problem wouldn't arise because the Directory Slash Redirect appends one, in the same way that users can type in www.example.com or www.example.com/ and get taken to the same place. But this only gets done when the directory actually exists.

So in a way it is a RegEx problem. You need another question mark

^/myurl/(\w+)/(\w+)/?(.*)

to allow for the requests for a "naked" directory. Since RegularExpressions are not retroactively greedy, you do not have to worry about the rule choosing to put the optional slash into capture #3 instead.

I will assume that your current URLs contain no hyphens, since those don't count as \w (although lowlines do).

g1smd

12:02 am on Oct 27, 2011 (gmt 0)

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



Did you add a rewrite or a redirect (both of those things can be coded using a RewriteRule)?

deesto

1:38 pm on Oct 27, 2011 (gmt 0)

10+ Year Member



g1smd: I want to retain the original request path, so I'm using a rewrite with a proxy flag [P].

lucy24: You're absolutely right: I was too quick to say this wasn't a regex problem.

So your suggested regex does work initially, and when the trailing slash is present in the original request:
^/myurl/(\w+)/(\w+)/?(.*) 


However, it breaks subsequent link requests when the trailing slash is omitted from the original request (skips a dir regex capture), so I'm still missing a capture or something in the redirect:
http://www.example.com/dirone/dirtwo/$1-$2/$3

lucy24

6:51 pm on Oct 27, 2011 (gmt 0)

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



In your rewritten version the slash between $1 and $2 has been replaced by a hyphen. If you want to retain the ability to capture both, say [-/] instead. (In vanilla regex, literal hyphens are supposed to be escaped if they are anywhere but the first thing in grouping brackets-- or first after ^ of course. RegEx may behave differently.) You will probably have to add a Condition to make sure you don't go into an infinite loop.