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>
3
  • @Drifter104 updated the question to specify what we did.
    – A. Man
    Mar 5, 2016 at 2:47
  • It appears using org.eclipse.jetty.server.handler.MovedContextHandler is the recommended way to do this. eclipse.org/jetty/documentation/9.0.6.v20130930/…
    – Drifter104
    Mar 5, 2016 at 10:29
  • We were indeed able to get it to work using MovedContextHandler. Thanks @Drifter104 !
    – A. Man
    Mar 18, 2016 at 23:53

1 Answer 1

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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .