visit
URL redirection, also known as URL forwarding, is a technique to give more than one URL address to a page, a form, or a whole Web site/application. HTTP has a special kind of response, called a HTTP redirect, for this operation.
Redirects accomplish numerous goals:In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have that start with 3, and a header holding the URL to redirect to.
When browsers receive a redirect, they immediately load the new URL provided in the
Location
header. Besides the small performance hit of an additional round-trip, users rarely notice the redirection.Permanent redirections
These redirections are meant to last forever. They imply that the original URL should no longer be used, and replaced with the new one.Search engine robots, RSS readers, and other crawlers will update the original URL for the resource.[1] The specification did not intend to allow method changes, but there are existing user agents that do change their method. was created to remove the ambiguity of the behavior when using non-
GET
methods.Temporary redirections
Sometimes the requested resource can't be accessed from its canonical location, but it can be accessed from another place. In this case, a temporary redirect can be used.
Special redirections
(Not Modified) redirects a page to the locally cached copy (that was stale), and (Multiple Choice) is a manual redirection: the body, presented by the browser as a Web page, lists the possible redirections and the user clicks on one to select it.
HTML redirections
HTTP redirects are the best way to create redirections, but sometimes you don't have control over the server. In that case, try a element with its attribute set to
Refresh
in the of the page. When displaying the page, the browser will go to the indicated URL.<head>
<meta http-equiv="Refresh" content="0; URL=//example.com/">
</head>
JavaScript redirections
Redirections in JavaScript are performed by setting a URL string to the property, loading the new page:window.location = "//example.com/";
Order of precedence
With three ways to trigger redirections, several ways can be used at the same time. But which is applied first?Domain aliasing
Ideally, there is one location, and therefore one URL, for each resource. But there are reasons for alternative names for a resource:Expanding the reach of your site
A common case is when a site resides at
www.example.com
, but accessing it from example.com should also work. Redirections for example.com
to www.example.com
are thus set up. You might also redirect from common synonyms or frequent typos of your domains.Moving to a new domain
For example, your company was renamed, but you want existing links or bookmarks to still find you under the new name.Forcing
Requests to the
//version
of your site will redirect to the //
version of your site.This technique does work for internal links, but try to avoid having internal redirects. A redirect has a significant performance cost (as an extra HTTP request occurs). If you can avoid it by correcting internal links, you should fix those links instead.
Temporary responses to unsafe requests
requests modify the state of the server and the user shouldn't resend them unintentionally.Typically, you don't want your users to resend , or requests. If you serve the response as the result of this request, a simple press of the reload button will resend the request (possibly after a confirmation message).In this case, the server can send back a (See Other) response for a URL that will contain the right information. If the reload button is pressed, only that page is redisplayed, without replaying the unsafe requests.Temporary responses to long requests
Some requests may need more time on the server, like (See Other) redirect that links to a page indicating that the action has been scheduled, and eventually informs about its progress, or allows to cancel it.
Apache
Redirects can be set either in the server config file or in the .htaccess of each directory.
The module has
Redirect
and RedirectMatch
directives that set up redirects by default:<VirtualHost *:443>
ServerName example.com
Redirect / //www.example.com
</VirtualHost>
The URL
//example.com/
will be redirected to //www.example.com/
, as will any files or directories under it (//example.com/some-page
will be redirected to //www.example.com/some-page
)RedirectMatch
does the same, but takes a to define a collection of affected URLs:RedirectMatch ^/images/(.*)$ //images.example.com/$1
All documents in the
images/
directory will redirect to a different domain.If you don't want a temporary redirect, an extra parameter (either the HTTP status code to use or the
permanent
keyword) can be used to set up a different redirect:Redirect permanent / //www.example.com
# …acts the same as:
Redirect 301 / //www.example.com
Nginx
In Nginx, you create a specific server block for the content you want to redirect:
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
To apply a redirect to a directory or only certain pages, use the
rewrite
directive:rewrite ^/images/(.*)$ //images.example.com/$1 redirect;
rewrite ^/images/(.*)$ //images.example.com/$1 permanent;
IIS
In IIS, you use the element to configure redirections.Most of the time this is a server problem, and if the server cannot detect it, it will send back a
Internal Server Error
. If you encounter such an error soon after modifying a server configuration, this is likely a redirection loop.Sometimes, the server won't detect it: a redirection loop can spread over several servers which each don't have the full picture. In this case, browsers will detect it and display an error message. Firefox displays:Firefox has detected that the server is redirecting the request for this address in a way that will never complete.…while Chrome displays:
This Webpage has a redirect loopIn both cases, the user can't do much (unless a corruption is happening on their side, like a mismatch of cache or cookies).It is important to avoid redirection loops, as they completely break the user experience.