title: FastAPI handling and redirecting 404
body: <p>How can i redirect a request with FastAPI if there is a HTTPException?</p>
  <p>In Flask we can achieve that like this:</p>
  <pre><code>@app.errorhandler(404)
  def handle_404(e):
      if request.path.startswith('/api'):
          return render_template('my_api_404.html'), 404
      else:
          return redirect(url_for('index'))
  </code></pre>
  <p>Or in Django we can use django.shortcuts:</p>
  <pre><code>from django.shortcuts import redirect
  
  def view_404(request, exception=None):
      return redirect('/')
  </code></pre>
  <p>How we can achieve that with FastAPI?</p>