I need to redirect all traffic without a subdomain to the www subdomain (e.g, foo.com->www.foo.com).
The code below is working, but i know redirecting code can be brittle and might bring security flaws.
Is this a proper way of achieving the above, or is there perhaps another set of directives i should use?
host("^(?!www).+".r) { h =>
unmatchedPath { p =>
schemeName { s =>
redirect(s"$s://www.$h$p", StatusCodes.MovedPermanently)
}
}
}
Edit: changed StatusCodes.SeeOther to StatusCodes.MovedPermanently
MY first error was using the wrong status code (
StatusCodes.SeeOtherinstead ofStatusCodes.MovedPermanently), the second was only using the path part of the urlThe version below passing my set of tests (see further below):
This passes the test below