Difference between revisions of "URL redirection"
Line 1: | Line 1: | ||
[[wikipedia:URL redirection]] | [[wikipedia:URL redirection]] | ||
+ | |||
+ | === HTTP status codes 3xx === | ||
+ | In the [[HTTP]] [[Protocol (computing)|protocol]] used by the [[World Wide Web]], a '''redirect''' is a response with a [[List of HTTP status codes|status code]] beginning with ''3'' that causes a browser to display a different page. If a client encounters a redirect, it needs to make a number of decisions how to handle the redirect. Different status codes are used by clients to understand the purpose of the redirect, how to handle caching and which request method to use for the subsequent request. | ||
+ | |||
+ | HTTP/1.1 defines several status codes for redirection (RFC 7231): | ||
+ | * [[HTTP 300|300 multiple choices]] (e.g. offer different languages) | ||
+ | * [[HTTP 301|301 moved permanently]] (redirects permanently from one URL to another passing link equity to the redirected page) | ||
+ | * [[HTTP 302|302 found]] (originally "temporary redirect" in HTTP/1.0 and popularly used for CGI scripts; superseded by 303 and 307 in HTTP/1.1 but preserved for backward compatibility) | ||
+ | * [[HTTP 303|303 see other]] (forces a GET request to the new URL even if original request was POST) | ||
+ | * [[HTTP 307|307 temporary redirect]] (provides a new URL for the browser to resubmit a GET or POST request) | ||
+ | * [[HTTP 308|308 permanent redirect]] (provides a new URL for the browser to resubmit a GET or POST request) | ||
+ | |||
+ | Status codes [[HTTP 304|304 not modified]] and [[HTTP 305|305 use proxy]] are not redirects. | ||
+ | |||
+ | {| class="wikitable" | ||
+ | |+ {{Anchor|Redirect_status_codes_and_characteristics}} Redirect status codes and characteristics<ref name="G1Lfc" /> | ||
+ | |- | ||
+ | ! HTTP Status Code !! HTTP Version !! Temporary / Permanent !! Cacheable !! Request Method Subsequent Request | ||
+ | |- | ||
+ | | 301 || HTTP/1.0 || Permanent || {{yes}} || GET / POST may change | ||
+ | |- | ||
+ | | 302 || HTTP/1.0 || Temporary || {{no2|not by default}} || GET / POST may change | ||
+ | |- | ||
+ | | 303 || HTTP/1.1 || Temporary || {{no|never}} || always GET | ||
+ | |- | ||
+ | | 307 || HTTP/1.1 || Temporary || {{no2|not by default}} || may not change | ||
+ | |- | ||
+ | | 308 || HTTP/1.1 || Permanent || {{yes2|by default}} || may not change | ||
+ | |- | ||
+ | |} | ||
+ | |||
+ | All of these status codes require the URL of the redirect target to be given in the Location: header of the HTTP response. The 300 multiple choices will usually list all choices in the body of the message and show the default choice in the Location: header. | ||
+ | |||
+ | ==== Example HTTP response for a 301 redirect ==== | ||
+ | |||
+ | A [[HTTP]] response with the 301 "moved permanently" redirect looks like this: | ||
+ | |||
+ | <syntaxhighlight lang="http"> | ||
+ | HTTP/1.1 301 Moved Permanently | ||
+ | Location: http://www.example.org/ | ||
+ | Content-Type: text/html | ||
+ | Content-Length: 174 | ||
+ | |||
+ | <html> | ||
+ | <head> | ||
+ | <title>Moved</title> | ||
+ | </head> | ||
+ | <body> | ||
+ | =Moved= | ||
+ | <p>This page has moved to <a href="http://www.example.org/">http://www.example.org/</a>.</p> | ||
+ | </body> | ||
+ | </html> | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== Using server-side scripting for redirection ==== | ||
+ | Web authors producing HTML content can't usually create redirects using HTTP headers as these are generated automatically by the web server program when serving an HTML file. The same is usually true even for programmers writing CGI scripts, though some servers allow scripts to add custom headers (e.g. by enabling "non-parsed-headers"). Many web servers will generate a 3xx status code if a script outputs a "Location:" header line. For example, in [[PHP]], one can use the "header" function: | ||
+ | |||
+ | <syntaxhighlight lang="php"> | ||
+ | header('HTTP/1.1 301 Moved Permanently'); | ||
+ | header('Location: http://www.example.com/'); | ||
+ | exit(); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | More headers may be required to prevent caching.<ref name="php-301-robust-solution" /> The programmer must ensure that the headers are output before the body. This may not fit easily with the natural flow of control through the code. To help with this, some frameworks for server-side content generation can buffer the body data. In the [[Active Server Pages|ASP scripting]] language, this can also be accomplished using <code>response.buffer=true</code> and <code>response.redirect <nowiki>"http://www.example.com/"</nowiki></code> HTTP/1.1 allows for either a relative URI reference or an absolute URI reference.<ref name="venDA" /> If the URI reference is relative the client computes the required absolute URI reference according to the rules defined in RFC 3986.<ref name="3Y1IG" /> | ||
+ | |||
+ | ==== Apache HTTP Server mod_rewrite{{anchor|mod_rewrite}} ==== | ||
+ | The [[Apache HTTP Server]] mod_alias extension can be used to redirect certain requests. Typical configuration directives look like: | ||
+ | <syntaxhighlight lang="apache"> | ||
+ | Redirect permanent /oldpage.html http://www.example.com/newpage.html | ||
+ | Redirect 301 /oldpage.html http://www.example.com/newpage.html | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | For more flexible [[URL rewriting]] and redirection, Apache mod_rewrite can be used. E.g., to redirect a requests to a canonical domain name: | ||
+ | <syntaxhighlight lang="apache"> | ||
+ | RewriteEngine on | ||
+ | RewriteCond %{HTTP_HOST} ^([^.:]+\.)*oldsite\.example\.com\.?(:[0-9]*)?$ [NC] | ||
+ | RewriteRule ^(.*)$ http://newsite.example.net/$1 [R=301,L] | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Such configuration can be applied to one or all sites on the server through the server configuration files or to a single content directory through a <code>[[.htaccess]]</code> file. | ||
+ | |||
+ | ==== nginx rewrite ==== | ||
+ | [[Nginx]] has an integrated http rewrite module,<ref name="T6lN6" /> which can be used to perform advanced URL processing and even web-page generation (with the <code>return</code> directive). A showing example of such advanced use of the rewrite module is [http://mdoc.su/ mdoc.su], which implements a deterministic [[URL shortening]] service entirely with the help of nginx configuration language alone.<ref name="ltnoQ" /><ref name="sjHzb" /> | ||
+ | |||
+ | For example, if a request for <code>[https://www.dragonflybsd.org/cgi/web-man?command=HAMMER§ion=5 /DragonFlyBSD/HAMMER.5]</code> were to come along, it would first be redirected internally to <code>/d/HAMMER.5</code> with the first rewrite directive below (only affecting the internal state, without any HTTP replies issued to the client just yet), and then with the second rewrite directive, an [[HTTP response]] with a [[HTTP 302|302 Found status code]] would be issued to the client to actually redirect to the external [[Common Gateway Interface|cgi script]] of web-[[man page|man]]:<ref name="y0ZUF" /> | ||
+ | <syntaxhighlight lang="nginx"> | ||
+ | location /DragonFly { | ||
+ | rewrite ^/DragonFly(BSD)?([,/].*)?$ /d$2 last; | ||
+ | } | ||
+ | location /d { | ||
+ | set $db "http://leaf.dragonflybsd.org/cgi/web-man?command="; | ||
+ | set $ds "§ion="; | ||
+ | rewrite ^/./([^/]+)\.([1-9])$ $db$1$ds$2 redirect; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
Revision as of 18:04, 26 August 2021
Contents
HTTP status codes 3xx
In the HTTP protocol used by the World Wide Web, a redirect is a response with a status code beginning with 3 that causes a browser to display a different page. If a client encounters a redirect, it needs to make a number of decisions how to handle the redirect. Different status codes are used by clients to understand the purpose of the redirect, how to handle caching and which request method to use for the subsequent request.
HTTP/1.1 defines several status codes for redirection (RFC 7231):
- 300 multiple choices (e.g. offer different languages)
- 301 moved permanently (redirects permanently from one URL to another passing link equity to the redirected page)
- 302 found (originally "temporary redirect" in HTTP/1.0 and popularly used for CGI scripts; superseded by 303 and 307 in HTTP/1.1 but preserved for backward compatibility)
- 303 see other (forces a GET request to the new URL even if original request was POST)
- 307 temporary redirect (provides a new URL for the browser to resubmit a GET or POST request)
- 308 permanent redirect (provides a new URL for the browser to resubmit a GET or POST request)
Status codes 304 not modified and 305 use proxy are not redirects.
HTTP Status Code | HTTP Version | Temporary / Permanent | Cacheable | Request Method Subsequent Request |
---|---|---|---|---|
301 | HTTP/1.0 | Permanent | Yes | GET / POST may change |
302 | HTTP/1.0 | Temporary | not by default | GET / POST may change |
303 | HTTP/1.1 | Temporary | never | always GET |
307 | HTTP/1.1 | Temporary | not by default | may not change |
308 | HTTP/1.1 | Permanent | by default | may not change |
All of these status codes require the URL of the redirect target to be given in the Location: header of the HTTP response. The 300 multiple choices will usually list all choices in the body of the message and show the default choice in the Location: header.
Example HTTP response for a 301 redirect
A HTTP response with the 301 "moved permanently" redirect looks like this:
<syntaxhighlight lang="http"> HTTP/1.1 301 Moved Permanently Location: http://www.example.org/ Content-Type: text/html Content-Length: 174
<html> <head> <title>Moved</title> </head> <body>
Moved
This page has moved to <a href="http://www.example.org/">http://www.example.org/</a>.
</body> </html> </syntaxhighlight>
Using server-side scripting for redirection
Web authors producing HTML content can't usually create redirects using HTTP headers as these are generated automatically by the web server program when serving an HTML file. The same is usually true even for programmers writing CGI scripts, though some servers allow scripts to add custom headers (e.g. by enabling "non-parsed-headers"). Many web servers will generate a 3xx status code if a script outputs a "Location:" header line. For example, in PHP, one can use the "header" function:
<syntaxhighlight lang="php"> header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.example.com/'); exit(); </syntaxhighlight>
More headers may be required to prevent caching.[2] The programmer must ensure that the headers are output before the body. This may not fit easily with the natural flow of control through the code. To help with this, some frameworks for server-side content generation can buffer the body data. In the ASP scripting language, this can also be accomplished using response.buffer=true
and response.redirect "http://www.example.com/"
HTTP/1.1 allows for either a relative URI reference or an absolute URI reference.[3] If the URI reference is relative the client computes the required absolute URI reference according to the rules defined in RFC 3986.[4]
Apache HTTP Server mod_rewriteScript error: No such module "anchor".
The Apache HTTP Server mod_alias extension can be used to redirect certain requests. Typical configuration directives look like: <syntaxhighlight lang="apache"> Redirect permanent /oldpage.html http://www.example.com/newpage.html Redirect 301 /oldpage.html http://www.example.com/newpage.html </syntaxhighlight>
For more flexible URL rewriting and redirection, Apache mod_rewrite can be used. E.g., to redirect a requests to a canonical domain name: <syntaxhighlight lang="apache"> RewriteEngine on RewriteCond %{HTTP_HOST} ^([^.:]+\.)*oldsite\.example\.com\.?(:[0-9]*)?$ [NC] RewriteRule ^(.*)$ http://newsite.example.net/$1 [R=301,L] </syntaxhighlight>
Such configuration can be applied to one or all sites on the server through the server configuration files or to a single content directory through a .htaccess
file.
nginx rewrite
Nginx has an integrated http rewrite module,[5] which can be used to perform advanced URL processing and even web-page generation (with the return
directive). A showing example of such advanced use of the rewrite module is mdoc.su, which implements a deterministic URL shortening service entirely with the help of nginx configuration language alone.[6][7]
For example, if a request for /DragonFlyBSD/HAMMER.5
were to come along, it would first be redirected internally to /d/HAMMER.5
with the first rewrite directive below (only affecting the internal state, without any HTTP replies issued to the client just yet), and then with the second rewrite directive, an HTTP response with a 302 Found status code would be issued to the client to actually redirect to the external cgi script of web-man:[8]
<syntaxhighlight lang="nginx">
location /DragonFly { rewrite ^/DragonFly(BSD)?([,/].*)?$ /d$2 last; } location /d { set $db "http://leaf.dragonflybsd.org/cgi/web-man?command="; set $ds "§ion="; rewrite ^/./([^/]+)\.([1-9])$ $db$1$ds$2 redirect; }
</syntaxhighlight>
AWS
- https://aws.amazon.com/premiumsupport/knowledge-center/elb-redirect-to-another-domain-with-alb/
- https://aws.amazon.com/premiumsupport/knowledge-center/route-53-redirect-to-another-domain/
- Using ALB:
AWS::ElasticLoadBalancingV2::Listener
, action: redirect
- Using ALB:
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-s3.html#scenario-s3-bucket-website
- https://aws.amazon.com/premiumsupport/knowledge-center/redirect-domain-route-53/
Related terms
- S3 bucket:
AWS::S3::Bucket
,AWS::S3::Bucket WebsiteConfiguration
- Nginx:
return
andrewrite
- 301 HTTP response status code. Redirects (300–399)
- Nginx return using subdomains
See also
- Web server: Nginx:
/etc/nginx/nginx.conf
,nginx -t
, Nginx logs, Nginx change log, PHP,php-fpm
, Let's encrypt, Nginx directives, Reverse Proxy, Configure HTTP redirection Nginx, Return,proxy_pass (Reverse proxy)
,ngx_http_rewrite_module
,/etc/nginx/sites-enabled/
,error.log
,access.log
,/nginx status
, AIO - Web, Search engine, How to add Wikipedia as a Chrome search engine, URL redirection, List of HTTP status codes, HAR (file format), HTTP response status code, URL, URI, Web3, reCAPTCHA
<ref>
tag;
no text was provided for refs named G1Lfc
<ref>
tag;
no text was provided for refs named php-301-robust-solution
<ref>
tag;
no text was provided for refs named venDA
<ref>
tag;
no text was provided for refs named 3Y1IG
<ref>
tag;
no text was provided for refs named T6lN6
<ref>
tag;
no text was provided for refs named ltnoQ
<ref>
tag;
no text was provided for refs named sjHzb
<ref>
tag;
no text was provided for refs named y0ZUF
Advertising: