JSF UrlRewriteFilter catchall/404 replacement

我们两清 提交于 2019-12-22 10:28:01

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/18240770/jsf-urlrewritefilter-catchall-404-replacement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!