I've tried different approaches on this and am losing the battle - perhaps someone here will have some guidance that could help me to be winning!

the url is normally like
arecipe.jsp?name=Cheese+Pizza&ingredient=Flour&culture=Italian&Type=

I would like that to look in the browser something like arecipe.jsp/name/Cheese+Pizza/ingredient/Flour/Culture/Italian
and then in the example type is empy i would want the url rewrite to be empty, so it wouldn't add the name,ingredient, culture if it didn't have anything assigned in the argument. or the other way around.

guess what i really mean is that my desire is to provide a link that looks like the second example but delivers the functionality i now get from the first. also, i want this to only work on some file names and not others. ugh.

so, if anyone looks at this and says 'oh, no brainer' i would be so grateful if you could show me the correct way to approach it.

Dani AI

Generated

Quick summary for : there are two practical ways to get URLs like what you described to behave like query strings without a visible redirect. The simplest and most robust (recommended when you already run Tomcat via mod_jk) is to let Tomcat receive the trailing path as PATH_INFO and parse it in the JSP/servlet. If you must transform the pretty path back into query parameters inside Apache, use mod_rewrite to build a conditional query string and pass the request to Tomcat with the PT flag so mod_jk still handles it.

Example: let Tomcat see path-info (recommended)

# in Apache config: make sure both the exact JSP and any trailing PATH_INFO are sent
JkMount /buncharecipes.jsp ajp13_worker
JkMount /buncharecipes.jsp/* ajp13_worker

Then in the JSP/servlet parse request.getPathInfo(), URL-decode each segment, and map pairs to names and values.

Example JSP parsing (kept short to show the idea):

<%
String path = request.getPathInfo(); // "/title/pistachio/type/pie"
if (path != null) {
  String[] p = path.split("/");
  java.util.Map params = new java.util.HashMap();
  for (int i=1; i+0 < p.length; i += 2) {
    String k = java.net.URLDecoder.decode(p[i],"UTF-8");
    String v = (i+1 < p.length) ? java.net.URLDecoder.decode(p[i+1],"UTF-8") : "";
    params.put(k,v);
  }
  request.setAttribute("parsedParams", params);
}
%>

If you prefer Apache to build query strings so existing JSP code can read request.getParameter(...), this mod_rewrite pattern sets only the keys that exist and does an internal pass-through to Tomcat:

RewriteEngine On

RewriteCond %{REQUEST_URI} /title/([^/]+)
RewriteRule ^ - [E=TITLE_P:title=%1&]

RewriteCond %{REQUEST_URI} /type/([^/]+)
RewriteRule ^ - [E=TYPE_P:type=%1&]

RewriteCond %{REQUEST_URI} /ingredient/([^/]+)
RewriteRule ^ - [E=INGREDIENT_P:ingredient=%1&]

RewriteRule ^/buncharecipes\.jsp /buncharecipes.jsp?%{ENV:TITLE_P}%{ENV:TYPE_P}%{ENV:INGREDIENT_P} [L,PT,QSA]

Notes and troubleshooting: do not use the R flag (that causes a visible redirect); include both JkMount /file.jsp and /file.jsp/* if you rely on PATH_INFO; use URL decoding in Tomcat (path segments normally use %20, not +); validate/sanitize all inputs; enable rewrite logging or test with curl to confirm the internal mapping.

OK, that was lame and unclear what I'm really looking for with mod_rewrite, I want to make sure I got it right, sorry.

SO if i have a link in a flat page that looks like

buncharecipes.jsp/title/pistachio
it will pass
buncharecipes.jsp?title=pistachio

but i also want buncharcipes.jsp/type/pie/ingredient/apple
to call buncharecipes.jsp?type=pie&ingedients=apple

without it doing some redirect type thing as far as the user can see.

there.

sorry for the verbosity.

I've tried different approaches on this and am losing the battle - perhaps someone here will have some guidance that could help me to be winning!

the url is normally like
arecipe.jsp?name=Cheese+Pizza&ingredient=Flour&culture=Italian&Type=

I would like that to look in the browser something like arecipe.jsp/name/Cheese+Pizza/ingredient/Flour/Culture/Italian
and then in the example type is empy i would want the url rewrite to be empty, so it wouldn't add the name,ingredient, culture if it didn't have anything assigned in the argument. or the other way around.

guess what i really mean is that my desire is to provide a link that looks like the second example but delivers the functionality i now get from the first. also, i want this to only work on some file names and not others. ugh.

so, if anyone looks at this and says 'oh, no brainer' i would be so grateful if you could show me the correct way to approach it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.