使用UrlRewriteFilter让Tomcat从http返回301重定向到https

lya*_*lex 5 https http-status-code-301 http-status-code-302 tuckey-urlrewrite-filter

我试图找出是否有人成功使用来自http://tuckey.org/urlrewrite/的UrlRewriteFilter可用于在Apache Tomcat中从http到https执行301永久重定向但我似乎无处可去快速.许多人都问同样的问题,AFAICS都没有被回答如果我问错地方了,那么也许有人会热情地向"重定向"我到正确的地方.如果不可能那么也许有人可以这样说.

谢谢.

    apache-tomcat-7.0.42
    jdk1.8.0_77
    CentOS Linux 7.2.1511
    urlrewritefilter-4.0.3.jar
Run Code Online (Sandbox Code Playgroud)

tomcat docs推荐的"标准"配置如下

    web.xml

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Secure URLs</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

    server.xml

    <Connector port="80" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="443" />

    <Connector port="443" maxThreads="150" scheme="https" secure="true"
           SSLEnabled="true" keystoreFile="/opt/keys/tomcat.keystore" 
           keystorePass="*********" clientAuth="false" keyAlias="tomcat" sslProtocol="TLS" />
Run Code Online (Sandbox Code Playgroud)

在浏览器中输入localhost会导致重定向到https,使用curl检查这一点我们可以看到这是按预期工作但我们得到302临时重定向

    root@sandbox:/tmp# curl -D /tmp/headers.txt -s  http://localhost

    HTTP/1.1 302 Found
    Server: Apache-Coyote/1.1
    Cache-Control: private
    Expires: Thu, 01 Jan 1970 01:00:00 GMT
    Location: https://localhost/
    Content-Length: 0
    Date: Fri, 29 Apr 2016 18:24:47 GMT
Run Code Online (Sandbox Code Playgroud)

然而,对于喜欢301永久性的谷歌而言,这是不可接受的

是否可以使用UrlRewriteFilter来实现这一目的

以下规则导致302,即使我正在使用type ="permanent-redirect",其他所有内容保持不变

    <rule>
       <name>seo redirect</name>
       <condition name="host" operator="notequal">^www_example_com</condition>
       <condition name="host" operator="notequal">^localhost</condition>
       <from>^/(.*)</from>
       <to type="permanent-redirect" last="true">https://www_example_com/$1</to>
    </rule>
Run Code Online (Sandbox Code Playgroud)

我尝试了各种不同的组合,但没有运气,因为Tomcat在应用过滤器后会重定向

有没有人真正让这个工作,所以我们得到301而不是302

谢谢