J Freeby
J Freeby

Reputation: 11

Apache Rewrite to a Location

We have a single web application that handles different URL formats and returns the same content in a different formats.

/dosomething
/foo/dosomething (returns mobile content)
/bar/dosomething (returns html for a browser)

See the sample Virtual Host below.

For Apache 2.2, I used Rewrite Rules to redirect http://www.foobarbaz.net/dosomething to the Location "/baz/" which functioned as I expected.

The "PT" flag on the RewriteRule passed the URI back to Apache and allowed us to serve content from the "baz" location.

<VirtualHost *:80>
    ServerName www.foobarbaz.net
    RewriteEngine On

    #Block requests for favicon
    RewriteRule ^/favicon.ico$  - [F] 

    # If the requested URI is NOT located in bar or foo
    # Prepend an /baz to everything that does not already starts with it
    # and force the result to be handled by the next URI-handler ([PT])
    RewriteCond %{REQUEST_URI} !^/bar/.*$
    RewriteCond %{REQUEST_URI} !^/foo/.*$
    RewriteRule ^/(.*) /baz/$1 [PT,QSA]

    <Location "/baz/">
        RequestHeader append ABC-Request-Origin "/baz"
        ProxyPass ajp://localhost:8009/webapp/
        ProxyPassReverse /
        ProxyPassReverseCookiePath /webapp /
    </Location>
    <Location "/bar/">
        RequestHeader append ABC-Request-Origin "/bar"
        ProxyPass ajp://localhost:8009/webapp/
        ProxyPassReverse /
        ProxyPassReverseCookiePath /webapp /bar/
    </Location>
    <Location "/foo/">
        RequestHeader append ABC-Request-Origin "/foo"
        ProxyPass ajp://localhost:8009/webapp/
        ProxyPassReverse /
        ProxyPassReverseCookiePath /webapp /foo/
    </Location>
</VirtualHost>

For Apache 2.4.12, the Rewrite Rules do not function in the same manner and Apache tries to find the content on the file system. Apache looks for a "baz" directory which does not exist.

I tried adding another location "/" and removing the rewrite rules (see below).

The docs indicate that for overlapping Locations, the least specific should go first.

http://httpd.apache.org/docs/current/sections.html

This works for "/", but not for "/foo/" or "/bar". It seems that "/" is always used and "foo" and "bar" are always included in the request to the p.roxy

<VirtualHost *:80>
    ServerName www.foobarbaz.net
    <Location "/">
        RequestHeader append ABC-Request-Origin "/baz"
        ProxyPass ajp://localhost:8009/webapp/
        ProxyPassReverse /
        ProxyPassReverseCookiePath /webapp /
    </Location>
    <Location "/bar/">
        RequestHeader append ABC-Request-Origin "/bar"
        ProxyPass ajp://localhost:8009/webapp/
        ProxyPassReverse /
        ProxyPassReverseCookiePath /webapp /bar/
    </Location>
    <Location "/foo/">
        RequestHeader append ABC-Request-Origin "/foo"
        ProxyPass ajp://localhost:8009/webapp/
        ProxyPassReverse /
        ProxyPassReverseCookiePath /webapp /foo/
    </Location>
</VirtualHost>

Does anyone have suggestions on how to improve this setup? Or suggestions on handling requests to "/" differently that "/foo" or "/bar"

Thanks for any help.

Jim

Upvotes: 1

Views: 2612

Answers (1)

J Freeby
J Freeby

Reputation: 11

It appears that using a Location "/" first does properly use the Locations for "/", "/foo" and "/bar" I do have other issues related to building links in the application, but I do not believe it is an Apache issue.

Upvotes: 0

Related Questions