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 “308 Temporary Redirect” status is used when responding. See this MDN article on why 308 is preferred over 301.

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

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

Shortcut for creating a redirect to use as a Responder.

Only receives a to argument since responders do not need to do route matching.

Use the “307 Temporary Redirect” status when responding.

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Convert self to HttpResponse.

Wraps responder to allow alteration of its response. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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