title: JSF - How to migrate URLs by intercepting and redirecting potential 404 errors?
body: <p>When migrating websites often dead URLs happen since old website used different URL structure or technology. Correct 301 &quot;permanent&quot; redirect is important for SEO friendly migrations.</p>
  <p><strong>Example:</strong></p>
  <p>Old page <code>https://example.com/product/coca-cola-bottle.html</code> moved in our JSF project to <code>https://example.com/p/coca-cola-1-l/1000</code></p>
  <p><strong>Concept</strong></p>
  <p>We will save those mappings in our database and add them to our JSF application.</p>
  <p>If an URL is requested that is not valid or rewritten we want to check the current requested URL if it exists in our database and redirect to the new target.</p>
  <p><strong>Some Ideas?</strong></p>
  <ul>
  <li>Create a custom exception handler and redirect if resource was not found</li>
  <li>Override navigation handler to handle it <em>before</em> exception handler</li>
  <li>Use <em>@WebFilter</em> with <em>DispatcherType.ERROR</em> and <em>404</em> error code check</li>
  <li>Use OCPSoft rewrite solution. Not sure how to do this without loading all mappings into configuration. I prefer dynamic (lazy) approach.</li>
  </ul>
  <p>Right now i implemented <em>@WebFilter</em> version using an injected bean to lookup the mapping.</p>