<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AEXT.NET MAGAZINE &#187; URL Rewriter</title>
	<atom:link href="http://aext.net/tag/url-rewriter/feed/" rel="self" type="application/rss+xml" />
	<link>http://aext.net</link>
	<description>How to Create a Website</description>
	<lastBuildDate>Wed, 30 May 2012 05:03:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Url Rewrite Filter</title>
		<link>http://aext.net/2008/07/url-rewrite-filter/</link>
		<comments>http://aext.net/2008/07/url-rewrite-filter/#comments</comments>
		<pubDate>Sat, 05 Jul 2008 03:49:00 +0000</pubDate>
		<dc:creator>Lief</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Filter]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[URL Rewriter]]></category>

		<guid isPermaLink="false">http://lamnguyenblog.com/2008/07/url-rewrite-filter/</guid>
		<description><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2008/07/url-rewrite-filter/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2008/07/url-rewrite-filter/" height="61" width="51" /></a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://aext.net/theme/platinoom" title="Platinoom - Premium WordPress Themes"><img src="http://aext.net/wp-content/uploads/2011/04/platinoom_wordpress_theme.png" alt="ThemeKiss" /></a></p><p style="float: right;"><a href="http://api.tweetmeme.com/share?url=http://aext.net/2008/07/url-rewrite-filter/&service=su.pr&service_api=b57727e991c454bd2b2c62ff71462c79"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://aext.net/2008/07/url-rewrite-filter/" height="61" width="51" /></a></p><p>Based on the popular and very useful <a href="http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html">mod_rewrite</a> for apache,      UrlRewriteFilter is a Java Web Filter for any J2EE compliant web application server      (such as <a href="http://caucho.com/">Resin</a>, <a href="http://www.orionserver.com/">Orion</a> or <a href="http://tomcat.apache.org/">Tomcat</a>), which allows you to rewrite URLs      before they get to your code. It is a very powerful tool just like Apache&#8217;s mod_rewrite.<br />
<span id="more-16"></span><br />
URL rewriting is very common with Apache Web Server (see <a href="http://httpd.apache.org/docs-2.0/misc/rewriteguide.html">mod_rewrite&#8217;s rewriting guide</a>) but has not      been possible in most java web application servers. The main things it is used for are:</p>
<ul>
<li>URL Tidyness / <a href="http://tuckey.org/urlrewrite/manual/2.6/guide.html#urlabs">URL Abstraction</a> &#8211; keep          URLs tidy irrespective of the underlying technology or framework (JSP, Servlet, Struts etc).</li>
<li>Browser Detection &#8211; Allows you to rewrite URLs based on request <abbr title="HyperText Transfer Protocol">HTTP</abbr> headers (such as user-agent or charset).</li>
<li>Date based rewriting &#8211; Allows you to forward or redirect to other URL&#8217;s based on the date/time (good for planned          outages).</li>
<li>Moved content &#8211; enable a graceful move of content or even a change in CMS.</li>
<li>Tiny/Friendly URL&#8217;s (i.e. blah.com/latest can be redirected to blah.com/download/ver1.2.46.2/setup.exe)</li>
<li>A Servlet mapping engine (see <a href="http://tuckey.org/urlrewrite/manual/2.6/guide.html#method">Method          Invocation</a>)</li>
</ul>
<p><span id="fullpost">UrlRewriteFilter uses an xml file, called urlrewrite.xml (it goes into the WEB-INF directory), for configuration.      Most parameters can be Perl5 style Regular Expressions or Wildcard Expressions. This makes it very powerful      indeed.</span></p>
<p><strong>Download</strong></p>
<p>Go to orignal site of URLRewriter and download sources code or Binaries, <a href="http://tuckey.org/urlrewrite/">http://tuckey.org/urlrewrite/</a>.</p>
<div class="profile-data"><strong><span id="fullpost">How to use?</span></strong></div>
<p><span id="fullpost">You can go to General Example page of Tuckey to see how to use <a href="http://tuckey.org/urlrewrite/manual/2.6/guide.html">URLRewriter</a> or following guide below.</span></p>
<p>Create your default web project and:</p>
<ol><span id="fullpost"></p>
<li>Download the zip and extract it into your          context&#8217;s directory ie, so that urlrewrite.xml goes into the WEB-INF directory.</li>
<li>Add the following to your WEB-INF/web.xml (add it near the top above your          servlet mappings (if you have any)):
<pre>&lt;filter&gt;
Â  &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
Â  &lt;filter-class&gt;org.tuckey.web.filters.urlrewrite.UrlRewriteFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
Â  &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
Â  &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre>
</li>
<li>Add your own configuration to the WEB-INF/urlrewrite.xml that was created.</li>
<li>Restart the context.</li>
<p></span></ol>
<p><span id="fullpost">You can visit <a href="http://localhost:8080/rewrite-status">http://localhost:8080/rewrite-status</a> (or whatever the address of your local webapp and context)      to see output (note: this page is only viewable from localhost).</span></p>
<p>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&#8217;s defined in urlrewrite.xml and beetween &lt;rule&gt; &#8230; &lt;/rule&gt;</p>
<ul><span id="fullpost"></p>
<li>Example:</li>
<p></span></ul>
<pre class="codetag"><span id="fullpost">&lt;rule&gt;
Â  &lt;from&gt;URL of the page which you want to rewrite&lt;/from&gt;
Â  &lt;to&gt;URL of the new page you cleaned&lt;/to&gt;
&lt;/rule&gt;</span></pre>
<div class="profile-data"><strong><span id="fullpost">Work with Servlet</span></strong></div>
<p><span id="fullpost">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:</span></p>
<pre><span id="fullpost">/servlets/exampleServlet?action=doExample</span></pre>
<p><span id="fullpost">In above, the action doExample will be performed inside example servlet. With URLRewite you can do that by:</span></p>
<pre><span id="fullpost">/example/doExample</span></pre>
<p><span id="fullpost">Following is code to use example, the request /example/doExample will be forwarded to /servlets/exampleServlet and      inside      the servlet request.getAttribute(&#8220;action&#8221;) will return doExample.</span></p>
<pre><span id="fullpost">&lt;rule&gt;
Â  &lt;from&gt;^/example/doExample$&lt;/from&gt;
Â  &lt;to&gt;/servlets/exampleServlet&lt;/to&gt;
Â  &lt;set name="action"&gt;doExample&lt;/set&gt;
&lt;/rule&gt;</span></pre>
<div class="profile-data"><strong><span id="fullpost">Method Invocation</span></strong></div>
<p><span id="fullpost">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.</span></p>
<p><strong>Invoke a servlet directly</strong></p>
<pre><span id="fullpost">&lt;rule&gt;
Â Â &lt;from&gt;^/example/doExample$&lt;/from&gt;
Â Â &lt;run class="yourpackage.ExampleServlet" method="doGet" /&gt;
&lt;/rule&gt;</span></pre>
<p><span id="fullpost">This will invoke doGet(HttpServletRequest request, HttpServletResponse response) when the &#8220;from&#8221; is      matched on a request. (remeber this method needs to be public!). You can use to invoke more than one methods by following example:</span></p>
<pre><span id="fullpost">&lt;rule&gt;
Â Â &lt;from&gt;^/example/doMethod1$&lt;/from&gt;
Â Â &lt;run class="yourpackage.ExampleServlet" method="method1" /&gt;
&lt;/rule&gt;

&lt;rule&gt;
Â Â &lt;from&gt;^/example/doMethod2$&lt;/from&gt;
Â Â &lt;run class="yourpackage.ExampleServlet" method="method2" /&gt;
&lt;/rule&gt;</span></pre>
<div class="profile-data"><strong><span id="fullpost">URL Abstraction</span></strong></div>
<p><span id="fullpost">Both incoming request and embedded links in JSP&#8217;s can be rewritten allowing full URL abstraction.</span></p>
<pre><span id="fullpost">&lt;rule&gt;
Â  &lt;from&gt;^/tidy/page$&lt;/from&gt;
Â  &lt;to&gt;/old/url/scheme/page.jsp&lt;/to&gt;
&lt;/rule&gt;

&lt;outbound-rule&gt;
Â  &lt;from&gt;^/old/url/scheme/page.jsp$&lt;/from&gt;
Â  &lt;to&gt;/tidy/page&lt;/to&gt;
&lt;/outbound-rule&gt;</span></pre>
<p><span id="fullpost">Any incoming requests for /tidy/page will be transparently forwarded to /old/url/scheme/page.jsp.</span></p>
<p>If you use JSTL your JSP page would have something like:</p>
<pre><span id="fullpost">&lt;a href="&lt;c:url value="/old/url/scheme/page.jsp"/&gt;"&gt;some link&lt;/a&gt;</span></pre>
<p><span id="fullpost">This will be rewritten upon output to:</span></p>
<pre><span id="fullpost">&lt;a href="/tidy/page"&gt;some link&lt;/a&gt;</span></pre>
<p><span id="fullpost">Or if you use standard JSP:</span></p>
<pre><span id="fullpost">&lt;a href="&lt;%= response.encodeURL("/old/url/scheme/page.jsp") %&gt;"&gt;some link&lt;/a&gt;</span></pre>
<p><span id="fullpost">Will generate output like:</span></p>
<pre><span id="fullpost">&lt;a href="/tidy/page"&gt;some link&lt;/a&gt;</span></pre>
<div class="profile-data"><strong><span id="fullpost">Other feature</span></strong></div>
<ul><span id="fullpost"></p>
<li>Tiny/Freindly url: when type /zebra brower wil send redirect to /big/ugly/url/1,23,56,23132.html</li>
<p></span></ul>
<pre><span id="fullpost">&lt;rule&gt;
Â Â &lt;from&gt;^/zebra$&lt;/from&gt;
Â Â &lt;to type="redirect"&gt;/big/ugly/url/1,23,56,23132.html&lt;/to&gt;
&lt;/rule&gt;
</span></pre>
<ul><span id="fullpost"></p>
<li>Default page as another: same as welcome-file in web.xml but is redirect.</li>
<p></span></ul>
<pre class="codetag"><span id="fullpost">&lt;rule&gt;
&lt;from&gt;^/$&lt;/from&gt;
&lt;to type="redirect"&gt;/opencms/opencms/index.html&lt;/to&gt;
&lt;/rule&gt;</span></pre>
<ul><span id="fullpost"></p>
<li>Disable access to a directory:
<ul>
<li>.htaccess in php with apache server:
<pre>Options -Indexes</pre>
</li>
<li>Use URLRewrite Filter:
<pre>&lt;rule&gt;
Â Â &lt;name&gt;Disable Directory&lt;/name&gt;
Â Â &lt;from&gt;^/notliveyet/.*$&lt;/from&gt;
Â Â &lt;to&gt;null&lt;/to&gt;
Â Â &lt;set type="status"&gt;403&lt;/set&gt;
&lt;/rule&gt;</pre>
</li>
</ul>
</li>
<li>Clean URL:
<ul>
<li>.htaccess in php with apache server:
<pre>RewriteRule ^worlds/([a-z]*)/([a-z]*)$ /world.jsp?country=$1&amp;city=$2 [L]</pre>
</li>
<li>Use URLRewrite Filter:
<pre>&lt;rule&gt;
Â Â &lt;from&gt;^/world/([a-z]+)/([a-z]+)$&lt;/from&gt;
Â Â &lt;to&gt;/world.jsp?country=$1&amp;amp;city=$2&lt;/to&gt;
&lt;/rule&gt;</pre>
</li>
</ul>
</li>
<p></span></ul>
<p><span id="fullpost">/world/unitedstates/newyork will be passed on to /world.jsp?country=unitedstates&amp;city=newyork</span></p>
<p>Other features go to <a href="http://tuckey.org/urlrewrite/manual/2.6/guide.html">guide page</a> at orginal site for help.</p>
<p>Best Rergad !</p>
<div class="blogger-post-footer">Nguyen, Lam D&#8217;s Bloger &#8211; http://prlamnguyen.blogspot.com</div>
]]></content:encoded>
			<wfw:commentRss>http://aext.net/2008/07/url-rewrite-filter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

