pub struct Redirect { /* private fields */ }
Expand description
An HTTP service for redirecting one path to another path or URL.
By default, the “307 Temporary Redirect” status is used when responding. See this MDN article on why 307 is preferred over 302.
§Examples
As service:
use actix_web::{web, App};
App::new()
// redirect "/duck" to DuckDuckGo
.service(web::redirect("/duck", "https://duck.com"))
.service(
// redirect "/api/old" to "/api/new"
web::scope("/api").service(web::redirect("/old", "/new"))
);
As responder:
use actix_web::{web::Redirect, Responder};
async fn handler() -> impl Responder {
// sends a permanent (308) redirect to duck.com
Redirect::to("https://duck.com").permanent()
}
Implementations§
source§impl Redirect
impl Redirect
sourcepub fn new(
from: impl Into<Cow<'static, str>>,
to: impl Into<Cow<'static, str>>,
) -> Self
pub fn new( from: impl Into<Cow<'static, str>>, to: impl Into<Cow<'static, str>>, ) -> Self
Construct a new Redirect
service that matches a path.
This service will match exact paths equal to from
within the current scope. I.e., when
registered on the root App
, it will match exact, whole paths. But when registered on a
Scope
, it will match paths under that scope, ignoring the defined scope prefix, just like
a normal Resource
or Route
.
The to
argument can be path or URL; whatever is provided shall be used verbatim when
setting the redirect location. This means that relative paths can be used to navigate
relatively to matched paths.
Prefer Redirect::to()
when using Redirect
as a responder since from
has
no meaning in that context.
§Examples
App::new()
// redirects "/oh/hi/mark" to "/oh/bye/johnny"
.service(Redirect::new("/oh/hi/mark", "../../bye/johnny"));
sourcepub fn to(to: impl Into<Cow<'static, str>>) -> Self
pub fn to(to: impl Into<Cow<'static, str>>) -> Self
Construct a new Redirect
to use as a responder.
Only receives the to
argument since responders do not need to do route matching.
§Examples
use actix_web::{web::Redirect, Responder};
async fn admin_page() -> impl Responder {
// sends a temporary 307 redirect to the login path
Redirect::to("/login")
}
sourcepub fn permanent(self) -> Self
pub fn permanent(self) -> Self
Use the “308 Permanent Redirect” status when responding.
See this MDN article on why 308 is preferred over 301.
sourcepub fn temporary(self) -> Self
pub fn temporary(self) -> Self
Use the “307 Temporary Redirect” status when responding.
See this MDN article on why 307 is preferred over 302.
sourcepub fn see_other(self) -> Self
pub fn see_other(self) -> Self
Use the “303 See Other” status when responding.
This status code is semantically correct as the response to a successful login, for example.
sourcepub fn using_status_code(self, status: StatusCode) -> Self
pub fn using_status_code(self, status: StatusCode) -> Self
Allows the use of custom status codes for less common redirect types.
In most cases, the default status (“308 Permanent Redirect”) or using the temporary
method, which uses the “307 Temporary Redirect” status have more consistent behavior than
301 and 302 codes, respectively.
// redirects would use "301 Moved Permanently" status code
Redirect::new("/old", "/new")
.using_status_code(StatusCode::MOVED_PERMANENTLY);
// redirects would use "302 Found" status code
Redirect::new("/old", "/new")
.using_status_code(StatusCode::FOUND);
Trait Implementations§
source§impl HttpServiceFactory for Redirect
impl HttpServiceFactory for Redirect
fn register(self, config: &mut AppService)
source§impl Responder for Redirect
impl Responder for Redirect
type Body = ()
source§fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>
HttpResponse
.Auto Trait Implementations§
impl Freeze for Redirect
impl RefUnwindSafe for Redirect
impl Send for Redirect
impl Sync for Redirect
impl Unpin for Redirect
impl UnwindSafe for Redirect
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)