title: How to make a Sprint Boot + Security application redirect host-relative
body: <p>I have an application running in a tomcat behind a apache instance. Requests from the apache are proxied via mod_rewrite since this is possible to configure in a .htaccess and I do not have access to vhost-configuration (mod_jk is not even installed).</p>
  
  <p>.htaccess looks like the following:</p>
  
  <pre><code>RewriteRule ^/?my-app/(.*)$ http://localhost:8080/my-app/$1 [P]
  </code></pre>
  
  <p>Thymeleaf nicely attaches the context-name to all URLs and generates relative ones like <code>/my-app/relative/path</code> (so without the <code>http://hostname</code>-part) thru-out the HTML, which work perfectly in this setup.</p>
  
  <p>Unfortunately as soon as I use Spring MVC to redirect something...</p>
  
  <pre><code>@RequestMapping("/redirect-to-relative-path")
  public String redirect() {
      return "redirect:/relative/path";
  }
  </code></pre>
  
  <p>...the users get redirected to <code>http://localhost:8080/my-app/relative/path</code> instead of just <code>/my-app/relative/path</code>, which will not work for outside users.</p>
  
  <p>Also Spring Security redirects not-yet-authorized users to <code>http://localhost:8080/my-app/login</code> instead of just <code>/my-app/login</code>.</p>
  
  <p>I know that I could just use a full URL to redirect to, but this should be dynamic.</p>
  
  <p>What is the best (spring-boot-)way to make the framework redirect to relative URLs instead of attaching the hostname? (...basically like Thymeleaf is doing it)</p>