URL rewriting is very common with Apache Web Server (see mod_rewrite's rewriting guide) but has not been possible in most java web application servers. The main things it is used for are:
- URL Tidyness / URL Abstraction - keep URLs tidy irrespective of the underlying technology or framework (JSP, Servlet, Struts etc).
- Browser Detection - Allows you to rewrite URLs based on request HTTP headers (such as user-agent or charset).
- Date based rewriting - Allows you to forward or redirect to other URL's based on the date/time (good for planned outages).
- Moved content - enable a graceful move of content or even a change in CMS.
- Tiny/Friendly URL's (i.e. blah.com/latest can be redirected to blah.com/download/ver1.2.46.2/setup.exe)
- A Servlet mapping engine (see Method Invocation)
Download
Go to orignal site of URLRewriter and download sources code or Binaries, http://tuckey.org/urlrewrite/.
How to use?
You can go to General Example page of Tuckey to see how to use URLRewriter or following guide below.Create your default web project and:
- Download the zip and extract it into your context's directory ie, so that urlrewrite.xml goes into the WEB-INF directory.
- Add the following to your WEB-INF/web.xml (add it near the top above your servlet mappings (if you have any)):
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
- Add your own configuration to the WEB-INF/urlrewrite.xml that was created.
- Restart the context.
When installed the filter, only file need to edit is urlrewrite.xml (it goes into the WEB-INF directory). Now, edit your file and see how to make the URLRewriter works. Each url was re-wroten, it's defined in urlrewrite.xml and beetween <rule> ... </rule>
- Example:
<rule>
<from>URL of the page which you want to rewrite</from>
<to>URL of the new page you cleaned</to>
</rule>
Work with Servlet
You can forward request from URL to servlet and set action of servlet you want to perform. For default if not use URLRewrite Filter, URL to perform action in servlet is:/servlets/exampleServlet?action=doExample
In above, the action doExample will be performed inside example servlet. With URLRewite you can do that by:/example/doExample
Following is code to use example, the request /example/doExample will be forwarded to /servlets/exampleServlet and inside the servlet request.getAttribute("action") will return doExample.<rule>
<from>^/example/doExample$</from>
<to>/servlets/exampleServlet</to>
<set name="action">doExample</set>
</rule>
Method Invocation
The standard servlet mapping that is done via web.xml is rather limiting. Only *.xxx or /xxxx/*, no abilty to have any sort of smart matching. Using UrlRewriteFilter any rule when matched can be set to run method(s) on a class.Invoke a servlet directly
<rule>
<from>^/example/doExample$</from>
<run class="yourpackage.ExampleServlet" method="doGet" />
</rule>
This will invoke doGet(HttpServletRequest request, HttpServletResponse response) when the "from" is matched on a request. (remeber this method needs to be public!). You can use to invoke more than one methods by following example:<rule>
<from>^/example/doMethod1$</from>
<run class="yourpackage.ExampleServlet" method="method1" />
</rule>
<rule>
<from>^/example/doMethod2$</from>
<run class="yourpackage.ExampleServlet" method="method2" />
</rule>
URL Abstraction
Both incoming request and embedded links in JSP's can be rewritten allowing full URL abstraction.<rule>
<from>^/tidy/page$</from>
<to>/old/url/scheme/page.jsp</to>
</rule>
<outbound-rule>
<from>^/old/url/scheme/page.jsp$</from>
<to>/tidy/page</to>
</outbound-rule>
Any incoming requests for /tidy/page will be transparently forwarded to /old/url/scheme/page.jsp.If you use JSTL your JSP page would have something like:
<a href="<c:url value="/old/url/scheme/page.jsp"/>">some link</a>
This will be rewritten upon output to:<a href="/tidy/page">some link</a>
Or if you use standard JSP:<a href="<%= response.encodeURL("/old/url/scheme/page.jsp") %>">some link</a>
Will generate output like:<a href="/tidy/page">some link</a>
Other feature
- Tiny/Freindly url: when type /zebra brower wil send redirect to /big/ugly/url/1,23,56,23132.html
<rule>
<from>^/zebra$</from>
<to type="redirect">/big/ugly/url/1,23,56,23132.html</to>
</rule>
- Default page as another: same as welcome-file in web.xml but is redirect.
<rule>
<from>^/$</from>
<to type="redirect">/opencms/opencms/index.html</to>
</rule>
- Disable access to a directory:
- .htaccess in php with apache server:
Options -Indexes
- Use URLRewrite Filter:
<rule> <name>Disable Directory</name> <from>^/notliveyet/.*$</from> <to>null</to> <set type="status">403</set> </rule>
- .htaccess in php with apache server:
- Clean URL:
- .htaccess in php with apache server:
RewriteRule ^worlds/([a-z]*)/([a-z]*)$ /world.jsp?country=$1&city=$2 [L]
- Use URLRewrite Filter:
<rule> <from>^/world/([a-z]+)/([a-z]+)$</from> <to>/world.jsp?country=$1&city=$2</to> </rule>
- .htaccess in php with apache server:
Other features go to guide page at orginal site for help.
Best Rergad !