سؤال

I'm setting up URL rules using Tuckey UrlRewrite. Everything to work so far, however I'm struggling with my default page.

Objective: - Any request not matching existing file; or - Any request not matching previous rules ...should launch a search thru search.jsf?q=. It's meant to handle any possible dead link from the legacy website and to replace the 404 page with something more functional (to help the user find what he's actually is looking for).

Partial code (the other rules are similar to the 2nd one, only the "default" rule makes it crash):

<rule>
  <name>Home</name>
  <from>^/$</from>
  <to type="forward" last="true">/home.jsf</to>
</rule>

<rule>
  <name>Contact Us</name>
  <from>^/contact_us/?$</from>
  <to type="forward" last="true">/contactUs.jsf</to>
</rule>

<rule>
  <name>Default + 404</name>
  <from>^/[^\s]+$</from>
  <to type="forward">^/search.jsf?q=$1</to>
</rule>

It causes a stack overflow as it matches search.jsf to [^\s]+ even though there is a physical file matching search.jsf.

Every other rule has last="true" as none of them should overlap (except this catchall, obviously).

I read the UrlRewriteFilter manual and couldn't seem to find anything besides last="true" which should, in theory, stop the process from checking for other matches if it already found one.

Thank you very much!

EDIT: With the lack of answers and my unability to solve this problem, I checked for an alternative way. Refer to this question here.

هل كانت مفيدة؟

المحلول

I know nothing of tuckey, but I can think of two simple solutions:

1 Create a rule for search like that rewrites a url like /search?q=foo to /search.jspf?q=foo

I am guessing something like:

<rule>
  <name>Search</name>
  <from>^/search\?(.*)$</from>
  <to type="forward" last="true">/search.jsf?\1</to>
</rule>

Then simply change your default rule to use /search instead of the actual file /search.jspf:

<rule>
  <name>Default + 404</name>
  <from>^/[^\s]+$</from>
  <to type="forward">^/search?q=$1</to>
</rule>

2 Rewrite your default rule's matching regex to specifically exclude search.jspf using a negative lookahead:

<rule>
  <name>Default + 404</name>
  <from>^/(?!search.jspf)[^\s]+$</from>
  <to type="forward">^/search.jspf?q=$1</to>
</rule>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top