Http path ing and redirection, and path ing and redirection

Source: Internet
Author: User

Http path ing and redirection, and path ing and redirection

It mainly includes Alias commands, AliasMatch, Redirect, RedirectMatch, ScriptAlias, and ScriptAliasMatch. They are provided by The mod_alias module, which is used to provide simple path ing and redirection requirements. For more complex content, see URL rewriting.

The usage of ScriptAlias is exactly the same as that of Alias. The only difference is that the target path of ScriptAlias ing is considered as a CGI file or a directory containing CGI files, which will be processed and executed by the cgi processor.

1.1 Processing order

Redirect and RedirectMatch commands take precedence over Alias and AliasMatch for processing. Redirect or Alias of the same type matches in the order of appearance, and the matching is immediately stopped.

For example, if several commands appear in the following order:

Redirect1Alias1AliasMatch2Alias3RedirectMatch2Redirect3

The matching sequence is Redirect1> RedirectMatch2> Redirect3> Alias1> AliasMatch2> alias3. When a match takes effect immediately, the following will be ignored.

1.2 Alias and AliasMatch

The Alias command maps the URL path to the path of the local file system, and the local path is not restricted by DocumentRoot. AliasMatch is the regular version of Alias.

Alias "/image" "/ftp/pub/image"

This will make the http: // myserver/image/foo.gif request get/ftp/pub/image/foo.gif, because it is precisely matched, so http: // myserver/imagefoo.gif does not return files in this path. However, Alias has a command AliasMatch that supports regular expression matching.

AliasMatch "(?i)^/image(.*)" "/ftp/pub/image$1"

In Alias, if the URL contains a trailing slash, The ing path should also contain a slash. For example, in the following example, if http: // myserver/icons is requested,/usr/local/apache/icons/$ DirectoryIndex will not be obtained.

Alias "/icons/" "/usr/local/apache/icons/"

After setting Alias, you need to consider whether the permission to map to the local path is allowed. Especially for paths outside DocumentRoot, these ing targets are usually inaccessible due to <Directory "/"> rejection. Therefore, for such a ing path, you must configure a <Directory> container for Alias instructions. For example:

Alias "/image" "/ftp/pub/image"<Directory "/ftp/pub/image">    Require all granted</Directory>

If Alias or AliasMatch appears in the <Location> container, they only need a ing path as the parameter, because the URL is already included in the container path. For example:

<Location "/image">    Alias "/ftp/pub/image"</Location>

1.3 Redirect and RedirectMatch commands

They inform the client that the resources have been transferred and ask them to resend the HTTP request for the new path resources.

Redirect [status] old-URL new-URL

The old URL must start with an absolute path "/", and the new URL must also be an absolute path, but it can be an absolute path containing scheme: // serverhost, it can also be an absolute path starting with a slash. For example:

# Redirect to Redirect "/service" "http://foo2.example.com/service" # Redirect to Redirect "/one" "/two" on the same host"

If the client requests a http://example.com/service/foo.txt, it is told to request a http://foo2.example.com/service/foo.txt again. Can be a URL containing the GET parameter, such as a http://example.com/service/foo.pl? Q = 23 & a = 42, will redirect to the http://foo2.example.com/service/foo.pl? Q = 23 & a = 42. However, the POST method is ignored.

If the status parameter is not specified, redirection is a temporary redirect (HTTP status 302 ). For the client, the path change of this resource is temporary. The following status parameters can return different HTTP status codes:

  • Permanent: returns a permanent redirect status code (301), indicating that the location change of the resource is permanent.
  • Temp: returns a temporary redirect status code (302), which is the default value.
  • Seeother: returns a "see" status code (303), indicating that the resource has been replaced.
  • Gone: returns an "obsolete" status code (410), indicating that the resource has been permanently deleted. If this status code is specified, the new-URL parameter is ignored.

Status can be specified as a value to return other specified status codes. If the value is between and, the new-URL parameter must be provided. Otherwise, the new-URL parameter must be ignored. However, the value must be a valid http status code value.

Like the alias command, if it is written in the Location container, the old-URL is ignored because it is already included in the location container path. For example:

<Location "/one">    Redirect permanent "http://example.com/two"</Location><Location "/three">    Redirect 303 "http://example.com/other"</Location><LocationMatch "/error/(?<NUMBER>[0-9]+)">    Redirect permanent "http://example.com/errors/%{env:MATCH_NUMBER}.html"</LocationMatch>
Go back to the Linux series article outline: workshop!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.