Vanity URL help

20 views
Skip to first unread message

grotto

unread,
Nov 26, 2009, 3:19:18 AM11/26/09
to UrlRewrite
I am trying to use UrlRewite to create vanity URL's - e.g., someone
types http://example.com/membername and the members profile is
displayed with the above url rather than http://example.com/member.jsp?id=3888

I can hard code this in the xml file but with 1000's of members I need
something that does this automatically.

Currently http://example.com/membername genrates a 404 and in the
404.jsp I check if it is a valid membername and then redirect to
member.jsp?id=3888 so the profile is displayed, but I want the
membername displayed in the url.

How do I go about this?

Do I need to call a servlet? Obviously I don't want to be checking for
membernames on valid url's. So it needs to be something like this: if
url is not a valid jsp or directory, check if it is a membername and
then redirect but leave the membername as the url, if not a membername
show 404 page.

Anyhelp with exmaples would be much appreciate.

Thanks

Wim De Smet

unread,
Nov 27, 2009, 4:03:51 AM11/27/09
to urlre...@googlegroups.com
Hi,

On Thu, Nov 26, 2009 at 9:19 AM, grotto <webm...@findlads.com> wrote:
> I am trying to use UrlRewite to create vanity URL's - e.g., someone
> types http://example.com/membername and the members profile is
> displayed with the above url rather than http://example.com/member.jsp?id=3888
>
> I can hard code this in the xml file but with 1000's of members I need
> something that does this automatically.
>
> Currently http://example.com/membername genrates a 404 and in the
> 404.jsp I check if it is a valid membername and then redirect to
> member.jsp?id=3888 so the profile is displayed, but I want the
> membername displayed in the url.
>
> How do I go about this?

It's usually better not to throw everything in a flat namespace. Why
not go for a full REST resource/identifier style? IOW, your member
URLs would be:
/member(s)/3888

You can support /members/3888/someuser if you want to. Or just
/members/someuser if "someuser" is guaranteed to be unique.

It all maps easily to an internal URL because you can just grab the ID
and you know what you are supposed to be displaying. If you really
want to be able to display on username I'd propose putting it in a
separate namespace as well. It could be as simple as /members/username
and just dealing with user name -> ID resolving in your web framework.
Because it says /members/ there you know where it is going and you
don't have to deal with things not getting hit by your code.

> Do I need to call a servlet? Obviously I don't want to be checking for
> membernames on valid url's. So it needs to be something like this: if
> url is not a valid jsp or directory, check if it is a membername and
> then redirect but leave the membername as the url, if not a membername
> show 404 page.
>
> Anyhelp with exmaples would be much appreciate.

One of the problems here is that determining whether a JSP exists or
not is at the very end of the chain. So you'd have to wait for the
default servlet to fire and then check whether it's returning a 404.
Possibly you could do all you want to in a filter, but I'm not sure if
there is really a way to return back down the stack with a request
once it's failed. Frankly, I don't think what you're trying to do maps
very well unto the web app model, though there is most likely a way to
make it work (I am not an expert by any measure :) ).

In any case, uwf is a very thin and comparatively dumb layer on top of
all this. It is generally the first thing that gets hit when a request
comes in. At this point, you have no idea whether you will be able to
serve this request or not. But you can redirect stuff. Typically, what
this would look like were I to implement it, would be something like
this rule:
<rule>
<from>^/members/(\w+)$</from>
<to>/members.jsp?id=$1</to> <!-- or replace members.jsp by whatever -->
</rule>

And then make sure the rest is intercepting requests that come in on
FORWARD, if you have any filters that need to process it.

I hope this makes the general patterns in which this is often handled
a bit more clear.

regards,
Wim

grotto

unread,
Nov 28, 2009, 1:36:56 PM11/28/09
to UrlRewrite
After spending a whole day working on this, I now have what I want.

I am using urlrewrite to call a servlet on the rule "/(\w+)$" which
checks if the url is a membername and if it is it forwards to the
member.jsp?id= leaving the orginal membername url displayed.

I tried to use a filter but tomcat url-pattern is limited and the
filter was getting invoked on everything so it was not very efficient.
The servlet is only called on /something

It was pretty simple in the end but just took lots of trial and error
to get it to work.

Thanks for your suggestions and help.
Reply all
Reply to author
Forward
0 new messages