url rewriting and dispatching
When you need to rewrite an external URL to a more convenient internal format, like rewriting to *.php files, you can use Resin's rewrite capabilities. Because Resin's rewrite mechanism contains much of the same functionality as Apache's mod_rewrite, you can translate older mod_rewrite rules to Resin's rewrite tags. Resin's dispatching is based on a list of dispatch rules configured in the resin-web.xml or the resin.xml configuration files. Each rule has a regular expression matching request URLs. The first dispatch rule that matches takes control of the request. For example, a <resin:Redirect> sends a HTTP redirect, and a <resin:Dispatch> dispatches the request as normal. Each matching rule can rewrite the URL using a target attribute. The target uses regexp replacement syntax like Perl's rewrite or sed. The following rule flips the first two segments around, so /foo/bar would become /bar/foo. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:Redirect regexp="^/([^/]+)/([^/]+)" target="/$2/$1"/> </web-app> Some dispatches might depend on request attributes like the security attribute. The <resin:IfSecure> tag checks if the request is an SSL request, i.e. if request.isSecure() is true. For non-SSL requests, the following <resin:Forbidden> applies. The rewrite conditions can all be used as security conditions, e.g. for <resin:Allow> or <resin:Deny>. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:Forbidden regexp="^/secret"> <resin:IfSecure value="false"/> </resin:Forbidden> </web-app> Basic ConditionsBasic conditions check the request and return true if the condition matches. Conditions can check on authentication (IsUserInRole), the remote IP (IfNetwork), check for SSL (IfSecure), and check for activation time (IfCron) or if a file exists (IfFileExists). The rewrite capability can also add standard predefined filters to modify the output, e.g. setting a response header. Filters can use conditions as restrictions, just like the dispatch rules. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:SetHeader regexp="^/secret" name="Foo" value="bar"/> </web-app> Servlet FiltersStandard servlet filters can also be invoked as an action to the Dispatch target. Your filter is created using Java Injection syntax and will be applied if the Dispatch rule matches. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:Dispatch regexp="^/test"> <mypkg:MyFilter xmlns:my="urn:java:com.foo.mypkg"> <mypkg:my-param>my-value</mypkg:my-param> </mypkg:MyFilter> </resin:Dispatch> </web-app>
Logging for the name <logger name="com.caucho.server.rewrite" level="finest"/> [1998/05/08 02:51:31.000] forward ^/foo: '/baz/test.jsp' no match [1998/05/08 02:51:31.000] forward ^/bar: '/baz/test.jsp' no match [1998/05/08 02:51:31.000] forward ^/baz: '/baz/test.jsp' --> '/hogwarts/test.jsp' Redirect http:// requests to https:// requestsThe desired behaviour is to redirect plain connections to SSL connections. http://anything.com/anything.html redirect => https://anything.com/anything.html <resin xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <cluster ...> <host ...> ... <resin:Redirect regexp="^" target="https://${host.name}"> <resin:IfSecure value="false"/> </resin:Redirect> ... </host> </resin> Make an alias for a web-app
The desired behaviour is to make it so that a web-app will match more than
one url pattern. For example, a web-app is deployed in
http://hostname/classroom/physics forward => http://hostname/physics http://hostname/classroom/physics/anything forward => http://hostname/physics/anything The rewrite-dispatch tag is used at the <host> level. If it was placed in a <web-app> then it would be too late to forward to a different web-app because Resin would have already resolved the web-app. <resin xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <cluster id=""> <host id=""> <resin:Forward regexp="^/classroom/physics" target="/physics"/> Forward based on host nameThe desired behaviour is to forward requests internally based on the host name. http://gryffindor.hogwarts.com/anything.html forward => /gryffindor/* http://slytherin.hogwarts.com/anything.html forward => /slytherin/anything.html http://hogwarts.com/anything.html forward => /anything.html The rewrite-dispatch tag is used at the <cluster> level. If it was placed in the <host> or the <web-app> then it would be too late to forward to a different host because Resin would have already resolved the host. <resin xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <cluster> ... <resin:Forward regexp="http://gryffindor\.[^/]+" target="/gryffindor/"/> <resin:Forward regexp="http://slytherin\.[^/]+" target="/slytherin/"/> ... </cluster> </resin>
|