URL rewriting with OcpSoft rewrite Join.path rule - jsf

I'm using Ocpsoft Rewrite, to perform URL rewriting in a JSF project. I have a redirect rule, which works fine:
.addRule()
.when(Direction.isInbound().and(Path.matches("/venue/{id}")))
.perform(Redirect.temporary(context.getContextPath() +
"/protected/index.xhtml?venueID={id}"));
However, because of the redirect, this changes the URL in the navigation bar. I thought I could use a Join rule instead, but it doesn't work as i expected:
.addRule(Join.path("/venue/{venueID}").to("/protected/index.xhtml"))
.perform(Log.message(Level.INFO, "Rewrite is active!"));
I thought this rule would redirect from, for example, foo/venue/123 to foo/protected/index.xhtml?venueID=123, but I don't get the ?venueID=... parameter appended to the URL.
Anyone knows what the correct rule should look like?

Your rule looks correct. But a Join doesn't result in a redirect. Instead it forwards the request internally. This means that the URL isn't changed. So you won't "see" the parameter venueID in the URL. But you will be able to read the parameter using the standard Servlet API:
String id = HttpServletRequest.getParameter("venueID");

If you really want, you can do a Forward instead:
.addRule()
.when(Direction.isInbound().and(Path.matches("/venue/{id}")))
.perform(Forward.to("/protected/index.xhtml?venueID={id}"));
But this won't handle outbound HTML link correction like Join will!

Related

How to correctly rewrite URLs?

I am a real noob in IIS URL rewrite module.
I want to rewrite all requests of
127.0.0.1/Content/[anything may come here]
to
127.0.0.1:7078/Content/[anything may come here]
I am very bad in regular expressions and I do not know how to do this
I tried using the wildcard feature in URL Rewrite module and did this:
Requested URL matches the pattern using wildcards
127.0.0.1/Scripts/*
then rewrite to
127.0.0.1:7078/Scripts/*
(Action Type is Rewrite)
I am attached a screenshot. I am not sure I am doing this the right way, because it is not working.
1/ You don't need the host part in the Match URL Pattern : /Scripts/* should work.
2/ If you want to match the wildcard in the Match URL Pattern to the Rewrite URL, you'll have to use something like {R:0} in the Rewrite URL. For example 127.0.0.1:7078/Scripts/{R:0}.
3/ I'm not sure you can rewrite to a different port. If rewrites does not work, try Redirect as Action Type instead.
If you want to hide port 7078, you might need to use Application Request Routing (Reverse Proxy).
Sources : http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module & http://forums.iis.net/t/1165389.aspx?URL+Rewrite+to+specific+port

.htaccess internal rewrite with preserving query string

i need to rewrite a url, preserve one query string for internal rewrite.
and another for display.
so this current url:
www.mysite.com/staff/teachers?id=37:john
needs to be internally rewritten to:
www.mysite.com/staff/teachers/37
not quite sure where to start, im trying this, but don't know how to access the number between "id=" and ":" to use for the rewrite
thanks for your help.
You cannot rewrite a URL "for display". All you can do is tell the browser to load a new URL. When the browser requests the new URL, the server has no way of knowing why it is being loaded (i.e. that it was the result of a previous redirect) so that new URL will need to include enough information to load the actual page.
In other words, you have to either:
include the ID in the "pretty" URL, such as www.mysite.com/staff/teachers/37-john
have your application look up the user based on their name in the internally rewritten URL, e.g. www.mysite.com/staff/teachers?lookup_name=john

IIS URL Rewrite - need help rewriting friendly urls for a forum

I'm new to using the URL Rewrite module and I'm having trouble with what I thought would be a simple URL rewrite for forum threads (using IIS 7.5)
I need to rewrite:
/forum/100/2534/friendly-title
or:
/forum/100/2534/334/comment/friendly-thread-title
to:
/forum/?forum=100&thread=2534&post=334&postType=comment
The rule that I have written (not working) is:
^forum/([1-9][0-9][0-9]*)/([1-9]*)/(([1-9]*)/(post|comment)/)?([a-zA-Z0-9-]{5,50})$
Which maps to:
/forum/?forum={R:1}&thread={R:2}&post={R:4}&postType={R:5}
I'm getting a 404 error.
It's correct that {R:4} and {R:5} are empty when you use the first URL. That's because there are no values for these fields. The RegEx still matches though so the URL will still be rewritten. Your code should properly handle empty values for the post and postType querystring parameters to display the entire thread and not just a specific comment (at least that what I assume is suppose to happen).
By the way, a more logical URL structure would be:
/forum/100/2534/friendly-thread-title/comment/334
This won't help you this this particular problem though but just on a side note.

Multiple and Variable parameters URL rewriting

I don't know how to rewrite URLs of this type:
mywebsite/param1-val1-param2-val2-param3-val3-param4-val4.html
that's really simple to do BUT my problem is that my parameters are variables like:
mywebsite/param1-val1-param3-val3-param4-val4.html
or
mywebsite/param3-val3-param4-val4.html
so, the number of parameters is not always the same. It can sometimes be just one, sometimes it can be 10 or more. It redirects to a search script which will grab the parameters through GET querystring.
What I want to do is to not write (on htaccess) a line for every link. The links are pretty simple in that form separated by a -(hyphen) sign.
Rather than rely on complex rewrite rules, I would suggest a simple rewrite rule and then modifying the code of your web application to do the hard part. Supporting this kind of variable parameters is not something that a rewrite rule is going to be very good at on its own.
I would use the following rewrite rule that intercepts any url that contains a hyphen separator and ends in .html
RewriteRule ^(.+[\-].+)\.html$ /query.html?params=$1
Then in your web application can get the parameters from the CGI parameter called params . They look like this now param1-val1-param3-val3-param4-val4. Your code should then split on the hyphens, and then put the parameters into a map. Most web frameworks support some way of adding to or overriding the request parameters. If so, you can do this without doing invasive modifications to the rest of your code.

htaccess remove part of the url

I have urls structured like this:
/!#/pretty-url/
I need to remove the !# so the urls will look like this:
/pretty-url/
What would be the correct .htaccess rule to do this?
When you have a # in the URL then that is a named anchor. The browser does not send anything to the right of a # to the server. This means that all the rewrite engine is seeing is:-
/!
If you want your URLs to look like this then you are going to need to use JavaScript to get the relevant content via AJAX.

Resources