URL redirection

From wikieduonline
Jump to navigation Jump to search

wikipedia:URL redirection

HTTP status codes 3xx[edit]

Moved[edit]

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[edit]

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.[1] 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.[2] If the URI reference is relative the client computes the required absolute URI reference according to the rules defined in RFC 3986.[3]

Apache HTTP Server mod_rewriteScript error: No such module "anchor".[edit]

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[edit]

Nginx has an integrated http rewrite module,[4] 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.[5][6]

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:[7]

location /DragonFly {
  rewrite ^/DragonFly(BSD)?([,/].*)?$ /d$2 last;
 }
 location /d {
  set $db "http://leaf.dragonflybsd.org/cgi/web-man?command=";
  set $ds "&section=";
  rewrite ^/./([^/]+)\.([1-9])$  $db$1$ds$2 redirect;
 }

AWS[edit]

Related terms[edit]

See also[edit]

  • Cite error: Invalid <ref> tag; no text was provided for refs named php-301-robust-solution
  • Cite error: Invalid <ref> tag; no text was provided for refs named venDA
  • Cite error: Invalid <ref> tag; no text was provided for refs named 3Y1IG
  • Cite error: Invalid <ref> tag; no text was provided for refs named T6lN6
  • Cite error: Invalid <ref> tag; no text was provided for refs named ltnoQ
  • Cite error: Invalid <ref> tag; no text was provided for refs named sjHzb
  • Cite error: Invalid <ref> tag; no text was provided for refs named y0ZUF
  • Advertising: