【问题标题】:How to permanently redirect JSP with parameters using urlRewriteFilter?如何使用 urlRewriteFilter 使用参数永久重定向 JSP?
【发布时间】:2015-07-28 21:37:04
【问题描述】:

我们正在将基于 JSP 的旧 Web 应用程序迁移到 Spring MVC 控制器并使用 urlRewriteFilter (http://tuckey.org/urlrewrite/) 进行重定向。

我们需要将带有参数的旧 JSP 永久重定向到控制器视图:

FROM: /products/product-detail.jsp?productId=666
TO: /product-detail?id=666

这里重要的是使用type="permanent-redirect",因为我们需要重定向对 SEO 友好。

我已经看过很多使用 urlRewriteFilter 的例子和帖子,所有这些都是处理不带参数的普通 JSP 的重写,例如some.jsp 到 /newurl

到目前为止,只有成就是使用前锋:

<rule>
    <name>Product Detail Old</name>
    <from>^/products/product-detail.jsp(.*)$</from>
    <to type="forward">/product-detail</to>
</rule>

但这当然根本不会重写 URL:

结果为:/products/product-detail.jsp?productId=666

我们也试过这个,但它不起作用:

<rule>
    <from>/products/product-detail.jsp?(.+)</from>
    <to type="permanent-redirect">/product-detail?$1</to>
</rule>

结果为@​​987654327@

有没有人可以帮助构建一个满足上述标准的规则 urlRewriteFilter?

帮助最感激。

【问题讨论】:

    标签: java redirect spring-mvc url-rewriting tuckey-urlrewrite-filter


    【解决方案1】:

    它不会自行重写...正如我在手册中发现的那样,&lt;from&gt; 仅使用 URL 字符串而没有查询字符串。 为了能够使用查询字符串,我们需要添加 &lt;urlrewrite use-query-string="true"&gt; 在这之后它就像一个魅力! 所以最后的规则:

    <urlrewrite use-query-string="true">
    
    <!-- more rules here if needed.... -->
    <rule>
        <from>^/products/product-detail.jsp\?productId=([0-9])$</from>
        <to type="permanent-redirect">/product-detail?id=$1</to>
    </rule>
    
    </urlrewrite>
    

    【讨论】:

    • 不错。这可能解释了我自己的问题。周末后去看看!您可以在手册中发布该条目的网址吗?干杯
    • @Lewdmole 那里有很棒的技能......我想我自己应该有那个 RTFM,但很高兴我们都得到了自己的答案!干杯!
    【解决方案2】:

    以下是否可行?

    <rule>
        <from>/products/product-detail.jsp\?productId=([0-9])</from>
        <to type="permanent-redirect">/product-detail?id=$1</to>
    </rule>
    

    编辑

    对不起。忘记了问号前面的斜线,这是正则表达式中的保留字符,除非您对其进行转义。

    这实际上让我昨天发现了,主要是直接从我们现有的旧 mod_rewrite 规则复制。

    【讨论】:

    • 感谢您的提示!这是正确的,只需要一个额外的设置来实现 - 请参阅下面的帖子。
    【解决方案3】:

    有几种方法可以解决这个问题。你可以使用

    <urlrewrite use-query-string="true">
    

    虽然这会影响文件中的所有规则。要针对特定​​规则的查询字符串,请使用条件元素

    <condition type="query-string">productId=([0-9])</condition>
    

    然后您可以像这样引用查询字符串

    <to type="permanent-redirect">/someUrl?%{query-string}</to>
    

    尽管在您的情况下,您希望更改查询字符串,因此您可能希望引用捕获的正则表达式

    <to type="permanent-redirect">/product-detail?id=$1</to>
    

    【讨论】:

      猜你喜欢
      • 2012-03-17
      • 2017-02-10
      • 1970-01-01
      • 2021-11-10
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多