Struct actix_web_lab::web::Redirect

source ·
pub struct Redirect { /* private fields */ }
👎Deprecated since 0.19.0: Type has graduated to Actix Web.
Expand description

An HTTP service for redirecting one path to another path or URL.

This feature has graduated to Actix Web. Further development will occur there.

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§

source§

impl Redirect

source

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");
source

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.

source

pub fn permanent(self) -> Self

Use the “308 Permanent Redirect” status when responding.

See this MDN article on why 308 is preferred over 301.

source

pub fn temporary(self) -> Self

Use the “307 Temporary Redirect” status when responding.

See this MDN article on why 307 is preferred over 302.

source

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 Clone for Redirect

source§

fn clone(&self) -> Redirect

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Redirect

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl HttpServiceFactory for Redirect

source§

fn register(self, config: &mut AppService)

source§

impl Responder for Redirect

source§

type Body = ()

source§

fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>

Convert self to HttpResponse.
source§

fn customize(self) -> CustomizeResponder<Self>
where Self: Sized,

Wraps responder to allow alteration of its response. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more