lyallex
lyallex

Reputation: 51

Using UrlRewriteFilter to get Tomcat to return a 301 redirect from http to https

I'm trying to find out if anyone has succeeded in using the UrlRewriteFilter availabe from http://tuckey.org/urlrewrite/ to do a 301 permanent redirect from http to https in Apache Tomcat but I don't seem to be getting anywhere fast. A number of people have asked the same question and AFAICS none have been answered If I'm asking in the wrong place then maybe someone would be kind enough to 'redirect' me to the right place. If it's not possible then perhaps someone could say so.

Thank you.

    apache-tomcat-7.0.42
    jdk1.8.0_77
    CentOS Linux 7.2.1511
    urlrewritefilter-4.0.3.jar

The 'standard' configuration as recommended by the tomcat docs is as follows

    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" />

entering localhost in a browser results in redirection to https checking this with curl we can see that this works as expected but we get 302 temporary redirect

    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

However this is unnacceptable to Google who prefer a 301 permanent

Is it possible to use UrlRewriteFilter to achieve this end

The following rule results in a 302 even though I'm using to type="permanent-redirect" everything else stays the same

    <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>

I have tried various different combinations with no luck presumably because Tomcat is redirecting after the filter has been applied

Has anyone actually got this to work so that we get a 301 instead of a 302

Thank You

Upvotes: 5

Views: 1758

Answers (0)

Related Questions