0

After following the suggestion on How to have Jetty redirect http to https, we were able to get our standalone Jetty deployment to redirect all HTTP requests to HTTPS. The redirect however is a 302. How can we make this a 301 (permanent) redirect instead?

For reference, we added this to Jetty's etc/webdefault.xml:

<web-app>
  ...
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Everything in the webapp</web-resource-name>
      <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
      <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
</web-app>
A. Man
  • 31

1 Answers1

2

We were able to get the desired behavior by adding a redirector.xml file next to the war. The contents of that file are below. Note that this will also redirect example.com to www.example.com:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure class="org.eclipse.jetty.server.handler.MovedContextHandler">
  <Set name="contextPath">/</Set>
  <Set name="newContextURL">https://www.example.com</Set>
  <Set name="permanent">true</Set>
  <Set name="discardPathInfo">false</Set>
  <Set name="discardQuery">false</Set>

  <Set name="virtualHosts">
    <Array type="String">
      <Item>example.com</Item>
    </Array>
  </Set>
</Configure>
A. Man
  • 31