Ric*_*k77 6 java ssl https tuckey-urlrewrite-filter url-redirection
我正在处理托管在Tomcat服务器上的Java Web应用程序.我必须设置从www到非www和从http到https的重定向.我想要以下三个网址:
重定向到
为此,我使用UrlRewriteFiltertuckey.org的4.0.3版.这是我的urlrewrite.xml档案:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
<rule>
<name>Redirect www to non-www and http to https</name>
<condition type="request-url" operator="equal">(^http://example.com|^http://www.example.com|^https://www.example.com)</condition>
<from>^(.*)$</from>
<to type="permanent-redirect" last="true">https://example.com$1</to>
</rule>
</urlrewrite>
Run Code Online (Sandbox Code Playgroud)
重定向工作但网站没有加载,浏览器显示消息:
这个页面不起作用
example.com重定向了你太多次了.
我使用了重定向检查器,发现在初次重定向到https://example.com/之后,另一个重定向到https://example.com/,然后是另一个,依此类推 - 该URL重定向到自身.我不明白是什么产生了这种无限循环.任何帮助,将不胜感激!
更新:我还没有解决方案.如果我从条件元素中删除第一个URL,则其他两个重定向工作正常,但问题是如何从http://example.com设置重定向.
我尝试了另一种方法 - 通过粘贴以下代码在web.xml文件中设置重定向到https:
<security-constraint>
<web-resource-collection>
<web-resource-name>all</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
Run Code Online (Sandbox Code Playgroud)
结果是相同的 - https://example.com在无限循环中重定向到自身.在这种情况下唯一的区别是重定向是状态代码302.任何关于导致此问题的原因以及如何解决它的想法?
更新:这是使用时curl命令的输出UrlRewriteFilter:
运行结果: curl http://example.com
HTTP/1.1 301 Moved Permanently
Server: nginx admin
Date: Fri, 04 May 2018 13:24:16 GMT
Content-Type: text/plain
Content-Length: 0
Connection: keep-alive
Location: https://example.com/
X-Cache: HIT from Backend
Run Code Online (Sandbox Code Playgroud)
运行结果: curl https://example.com/
HTTP/1.1 301 Moved Permanently
Date: Fri, 04 May 2018 11:58:51 GMT
Server: Apache-Coyote/1.1
Location: https://example.com/
Content-Length: 0
Content-Type: text/plain
Run Code Online (Sandbox Code Playgroud)
我会像下面这样简化规则
<rule>
<name>Ensure HTTPS</name>
<condition type="scheme" operator="notequal" next="or">https</condition>
<condition name="host" operator="notequal">www.example.com</condition>
<from>^/(.*)$</from>
<to type="redirect">https://example.com/$1</to>
</rule>
Run Code Online (Sandbox Code Playgroud)
另请确保证书有效example.com并在下面的线程中指出
UrlRewriteFilter:www 和 https 重定向
| 归档时间: |
|
| 查看次数: |
903 次 |
| 最近记录: |