Url rewriting Tuckey过滤器不适用于主机不相等的多个条件

Url rewriting Tuckey过滤器不适用于主机不相等的多个条件,url-rewriting,tuckey-urlrewrite-filter,Url Rewriting,Tuckey Urlrewrite Filter,当我的URL为localhost:8080时,下面关于Tuckey UrlRewriteFilter的规则错误地总是导致localhost:8080重定向到www.example.com 对于的这种行为似乎与Tuckey过滤器参考手册相反 我想要的是localhost:8080在不重定向的情况下保持不变,以允许在本地计算机上进行测试 我希望避免不在example.com域中的不需要的URL被搜索引擎索引。不需要的URL具有不同的域,但指向相同/重复的example.com页面 <urlre

当我的URL为localhost:8080时,下面关于Tuckey UrlRewriteFilter的规则错误地总是导致localhost:8080重定向到www.example.com

对于的这种行为似乎与Tuckey过滤器参考手册相反

我想要的是localhost:8080在不重定向的情况下保持不变,以允许在本地计算机上进行测试

我希望避免不在example.com域中的不需要的URL被搜索引擎索引。不需要的URL具有不同的域,但指向相同/重复的example.com页面

<urlrewrite>
    <rule>
        <name>Avoid wrong hostname's pages being indexed by search engines</name>

        <condition name="host" operator="notequal" next="and">www.example.com</condition>
        <condition name="host" operator="notequal" next="and">localhost:8080</condition>

        <from>^/(.*)</from>
        <to type="permanent-redirect" last="true">http://www.example.com/$1</to>
    </rule>

避免错误的主机名页面被搜索引擎索引
www.example.com
本地主机:8080
^/(.*)
http://www.example.com/$1

备选方案:

我还尝试了另一种方法:删除所有条件元素,并将“from”改为:

^/(^www.example.com | ^localhost:8080)(\?*)$

i、 e.不等于example.com,也不等于localhost——但这有同样的问题。

我也有同样的问题,但无法使用tuckey找到解决方案。我最终通过在Spring中使用拦截器解决了localhost测试和域名一致性的兼容性问题。我的代码是这样的

public boolean preHandle(HttpServletRequest request,
    HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURL().toString();
if (!url.startsWith("http://localhost") && !url.startsWith("http://www.example.com")){
    response.sendRedirect("http://www.example.com"+request.getRequestURI());
    return false;
}
return true;}

但是,检查每个请求都会有必要的开销。希望这有帮助

当您使用正则表达式匹配类型时,请尝试将条件也设置为正则表达式。例如,
^www.example.com$

为我工作。我更喜欢永久重定向,而不是response.sendRedirect(…)提供的临时重定向。因此,我将if语句的主体设置为:response.setStatus(HttpServletResponse.SC_永久移动);response.setHeader(“Location”,”);return false;谢谢。我不再参与该项目,因此无法尝试您的建议。(我的理解是,在Tuckey UrlRewriteFilter中,name=“host”是一个完整的主机名,如www.example.com或example.com so regex(通常用于URL)不需要吗?不过,我不确定;你的建议值得别人尝试。)
public boolean preHandle(HttpServletRequest request,
    HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURL().toString();
if (!url.startsWith("http://localhost") && !url.startsWith("http://www.example.com")){
    response.sendRedirect("http://www.example.com"+request.getRequestURI());
    return false;
}
return true;}