Cmdr_Tofu has asked for the wisdom of the Perl Monks concerning the following question:

Humbly seeking pardon for asking a question which might be perceived as offtopic to some.

This aspiring perl-guru needs to redirect requests for certain directories (with the exception of some subtrees) to a different webserver. Examples will make this clear.

Requests to http://server1/basedirectory should go to http://server2/basedirectory

Likewise we need the following:
http://server1/basedirectory/something -> http://server2/basedirectory/something
etc.

However we need to omit some urls from the rule: http://server1/basedirectory/exceptiondirectory1 and http://server1/basedirectory/exceptiondirectory2 (and their subtrees) should stay on the same server and not get redirected.

We have apache 1.3.26 compiled with both mod_alias and mod_rewrite, and we are currently using a very ugly solution to this problem, the following rule:
RedirectMatch ^/basedirectory/([^e])(.*)$ http://server2/basedirectory +/$1$2

This will will match our exception directories and then some extra unwanted stuff too.

What I really want is something like what perl supports. Is there a way to do something like the following:
RedirectMatch ^/basedirectory/(?!exceptiondirectory1|exceptiondirector +y2)(.*)$ http://server2/basedirectory/$1$2

Thanks in advance to any who can advise me,

Rohit

Replies are listed 'Best First'.
(OT) Re: apache RedirectMatch (regexp question)
by Kanji (Parson) on Sep 27, 2002 at 17:00 UTC

    I don't think you can easily (and certainly not gracefully) accomplish what you want to do with RedirectMatch, but mod_rewrite is excellent for this kind of voodoo...

    RewriteEngine On RewriteRule !^/basedirectory/(exceptions|to|the|rule).* - [C] RewriteRule ^/(basedirectory/.*) http://server2/$1

        --k.


      Thank you very much. This works beautifully :)
[OT] Re: apache RedirectMatch (regexp question)
by DaveH (Monk) on Sep 29, 2002 at 09:52 UTC

    Hi

    Apache processes the RedirectMatch statements from the top of the config file, to the bottom, so the 1st rule is matched first, 2nd rule second, etc, etc (as I'm sure you know).

    What this means is that you can add all of your exceptions *above* your original regex, and they will get matched first. Unfortunately, you also need some way of stopping the rules matching again, after the redirect has been followed. This is why I have used the double '//' in the URLs (this does mean that exceptions will look ugly once the redirect has been followed). e.g.

    RedirectMatch ^/basedirectory/exceptiondirectory1(.*)$ http://server1/ +basedirectory//exceptiondirectory1$1 RedirectMatch ^/basedirectory/exceptiondirectory2(.*)$ http://server1/ +basedirectory//exceptiondirectory2$1 RedirectMatch ^/basedirectory/(|[^/].*)$ http://server2/basedirectory/ +$1

    This will work, but is a bit messy (and doesn't overly improve on your original suggestion).

    What would be better is if the problem could be flipped, and you set the redirects up on server2 (but if you don't admin that server, I can see that would be a problem). Then all you would need is:

    RedirectMatch ^/basedirectory/exceptiondirectory1(.*)$ http://server1/ +basedirdirectory/exceptiondirectory1$1 RedirectMatch ^/basedirectory/exceptiondirectory2(.*)$ http://server1/ +basedirectory/exceptiondirectory2$1

    HTH,

    -- Dave :-)