Struct actix_web_lab::web::Redirect
source · [−]pub struct Redirect { /* private fields */ }
Expand description
An HTTP service for redirecting one path to another path or URL.
Redirects are either relative or absolute.
By default, the “307 Temporary Redirect” status is used when responding. See this MDN article on why 307 is preferred over 302.
Examples
use actix_web::{web, App};
use actix_web_lab::web as web_lab;
App::new()
// redirect "/duck" to DuckDuckGo
.service(web_lab::Redirect::new("/duck", "https://duckduckgo.com/"))
.service(
// redirect "/api/old" to "/api/new" using `web::redirect` helper
web::scope("/api").service(web_lab::redirect("/old", "/new"))
);
Implementations
sourceimpl 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
Create a new Redirect
service, first providing the path that should be redirected.
The default “to” location is the root path (/
). It is expected that you should call either
to
or to
afterwards.
Note this function has no effect when used as a responder.
Redirect to an address or path.
Whatever argument is provided shall be used as-is when setting the redirect location. You can also use relative paths to navigate relative to the matched path.
Examples
// redirects "/oh/hi/mark" to "/oh/bye/mark"
Redirect::new("/oh/hi/mark", "../../bye/mark");
sourcepub fn to(to: impl Into<Cow<'static, str>>) -> Self
pub fn to(to: impl Into<Cow<'static, str>>) -> Self
Shortcut for creating a redirect to use as a Responder
.
Only receives a to
argument since responders do not need to do route matching.
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 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
sourceimpl HttpServiceFactory for Redirect
impl HttpServiceFactory for Redirect
fn register(self, config: &mut AppService)
sourceimpl Responder for Redirect
impl Responder for Redirect
type Body = ()
sourcefn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>
Convert self to HttpResponse
.
sourcefn customize(self) -> CustomizeResponder<Self>
fn customize(self) -> CustomizeResponder<Self>
Wraps responder to allow alteration of its response. Read more
Auto Trait Implementations
impl RefUnwindSafe for Redirect
impl Send for Redirect
impl Sync for Redirect
impl Unpin for Redirect
impl UnwindSafe for Redirect
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more