. htaccess Rewrite rules detailed description _linux

Source: Internet
Author: User
Tags parent directory response code

Domestic virtual machines are generally not available. htaccess function (as far as I know, Discuz's host seems to provide this function), while in foreign hosts,. htaccess function seems to be standard, the author of the blog frame on MT, support. htaccess, every time I see a bunch of other people have written. htaccess settings, very Many orders are not, see, modify it is inconvenient, learn from the bitter, painstaking study, know why ~

Learning premise: (Not a friend to learn, in order to better understand the following text)

Linux Basics (No, it's all right, write a.) htaccess no need to bother, recommend: Brother Bird private kitchen Linux Basics)

Regular expressions (rewrite rules are based on a regular basis, recommended: Regular Expressions 30-minute introductory tutorial)

Syntax format for rewrite:

Copy Code code as follows:

Rewriteengine on #要想rewrite起作用, must be written on OH
Rewritebase Url-path #设定基准目录, such as Rewrtie to the file under the root directory, is the "/"
Rewritecond test-string Condpattern #写在RewriteRule之前, you can have one or n, used to test rewrite matching conditions, specific how to write, later will be detailed.
Rewriterule Pattern Substitution #规则

Rewriteengine on| Off

Rewriteengine is used to turn on or deactivate the rewrite feature.
Rewrite configurations does not inherit automatically, so you have to add this instruction to each virtual host directory that you want to use rewrite functionality.

Rewritebase Url-path

Rewritebase is used to set the base URL for the override. In the following section, you can see that rewriterule can be used in directory-level configuration files (. htaccess) and in a local scope, that is, the rules actually handle only part of the local path prefix being stripped. After the process is finished, the path is automatically attached back. The default value is "Rewritebase physical-directory-path".
When a new URL is replaced, the module must inject the URL back into the server process. To do this, it must know its corresponding URL prefix or URL reference. Typically, the prefix is the corresponding file path. However, most web site URLs do not directly correspond to their physical file path, and therefore generally do not make such an assumption! So in this case, you must use the Rewritebase directive to specify the correct URL prefix.
If your site server URL does not correspond directly to the physical file path, and you need to use the Rewritebase directive, you must specify the Rewriterule in each of the corresponding. htaccess files.

Rewritecond teststring Condpattern [flags]

The Rewritecond directive defines the condition of a rule that has one or more rewritecond instructions before a rewriterule instruction. The overriding rule after a condition works only if the current URI matches the pattern and meets these conditions.
TestString is a plain-text string, but it can also contain the following extensible components:

Rewriterule Reverse Reference: The Reference method is $N (0 <= N <= 9) that references the grouping elements (parentheses!) that match pattern in the rewriterule of the current (with a number of rewritecond directives).
Rewritecond Reverse Reference: The Reference method is%N (1 <= N <= 9) that refers to the grouping elements (parentheses!) in the conditions that are last met in some of the current rewritecond conditions.
Rewritemap Extension: Reference method is ${mapname:key|default}
Server variables: Reference method is%{name_of_variable} This is the function we use most often
name_of_variable specific values are shown in the following table:

HTTP headers: Connection & Request:
Http_user_agent
Http_referer
Http_cookie
http_forwarded
Http_host
Http_proxy_connection
Http_accept
Remote_addr
Remote_host
Remote_user
Remote_ident
Request_method
Script_filename
Path_info
Query_string
Auth_type
Server Internals: System stuff: Specials:
Document_root
Server_admin
server_name
Server_addr
Server_port
Server_protocol
Server_software
Time_year
Time_mon
Time_day
Time_hour
Time_min
Time_sec
Time_wday
Time
Api_version
The_request
Request_uri
Request_filename
Is_subreq

These correspond to similar named HTTP MIME headers, c variables for Apache servers, and struct TM fields in Unix systems, most of which are described in other manuals or CGI specifications. And the variables that are unique to mod_rewrite are:
Is_subreq
If the request being processed is a child request, it contains the string "true", otherwise it is "false". The module may produce a child request in order to resolve additional files in the URI.
api_version
This is the version of the Apache module API for the HTTPD (internal interface between servers and modules) in use, and is defined in Include/ap_mmn.h. This module version corresponds to the version of Apache that is being used (for example, in the Apache 1.3.14 release, this value is 19,990,320:10). Typically, it is the author of the module that is interested in it.
the_request
This is the complete HTTP request line sent by the browser to the server. (For example, "get/index.html Http/1.1″"). It does not contain additional header information sent by any browser.
Request_uri
This is the resource requested in the HTTP request line. (For example, "/index.html" in the example above.)
Request_filename
This is the file path name or description of the complete local file system that matches the request.

Condpattern is conditional pattern, that is, a regular expression applied to the current instance teststring, that is, teststring will be computed and then matched to Condpattern.

Note: Condpattern is a regular expression compatible with Perl, but there are several additions:

You can use the '! ' character (exclamation) in the pattern string to achieve a matching reversal.

Rewriteoptions Options

The rewriteoptions directive sets some options for the current server-level and directory-level configuration. Option can be one of the following values:

Inherit
This value forces the current configuration to inherit its parent configuration. In a virtual host-level configuration, it means that the primary server's mapping tables, conditions, and rules can be inherited. In a directory-level configuration, it means that the conditions and rules in the. htaccess of its parent directory can be inherited.
Maxredirects=number
To avoid the endless internal redirection of directory-level Rewriterule, mod_rewrite will stop processing this request when the class redirects and 500 internal server errors reach a maximum value. You can increase this value if you do need to allow an internal redirect greater than 10 times per request.

Rewriterule pattern substitution [flags]

The rewriterule directive is the root of the rewrite engine. This directive can be used more than once. Each instruction defines a simple rewrite rule. The order in which these rules are defined is particularly important because, at runtime, rules are enforced in that order.

Pattern is a Perl-compatible regular expression that acts on the current URL.

In addition, you can use the no character ('! ') Pattern prefix to achieve the inversion of pattern. However, it is necessary to note that when using no character to reverse pattern, the composition of the grouping is not used in pattern, i.e. $n.

The substitution in an overriding rule is a string that replaces (or replaces) the original URL when it matches the pattern. In addition to plain text, you can also use the

$N reverse-referencing rewriterule pattern
%N reverse references the last matching rewritecond pattern
Server variable for rule condition test string (%{varname})
mapping function call (${mapname:key|default})

Here are a few complete examples for you to refer to:

First, anti-theft chain function

Only these four lines on the realization of the chain is not very magical ^_^, writing up is not again feel complicated.

Copy Code code as follows:

Rewriteengine on
Rewritecond%{http_referer}!^http://(. +.)? mysite.com/[NC]
Rewritecond%{http_referer}!^$
Rewriterule. *. (jpe?g|gif|bmp|png) $/images/nohotlink.jpg [L]

Second, the Web site standardization
This is an example of redirecting all two-level domain names to www.yourdomain.com, but is it now so simple?

Copy Code code as follows:

Options +followsymlinks
Rewriteengine on
Rewritecond%{http_host} ^yourdomain.com [NC]
Rewriterule ^ (. *) $ http://www.yourdomain.com/$1 [r=301,l]

Third, temporary error page
When your site is upgraded or modified, you'd better let the visitor go to the specified page, not the unfinished page or the error page.

Copy Code code as follows:

Rewriteengine on
Rewritecond%{request_uri}!/maintenance.html$
Rewritecond%{remote_addr}!^123.123.123.123
Rewriterule $/error.html [r=302,l]

Iv. redirect RSS addresses to Feedsky
In addition to changing the RSS address in the template, the. htaccess can also make changes to the RSS address, and more convenient.

Copy Code code as follows:

Rewriteengine on
Rewritecond%{http_user_agent}! FeedSky [NC]
Rewritecond%{http_user_agent}! Feedvalidator [NC]
Rewriterule ^feed/? ([_0-9a-z-]+)?/?$ http://feed.feedsky.com/yours

Appendix: Flags

' Redirect| R [=code] ' (Force redirect redirect)

Substitution that are prefixed with http://thishost[:thisport]/(making the new URL a URI) can be enforced by an external redirect. If code is not specified, an HTTP response code of 302 (temporary move) is generated. If you need to use another response code in the range 300-400, just specify this value here, and you can also use one of the following symbol names: Temp (default), permanent, seeother. It can be used to feedback the normalized URL to the client, such as rewriting "/~" as "/u/", or/u/user plus slashes, and so on. Note: When using this tag, you must ensure that the replacement field is a valid url! Otherwise, it will point to an invalid location! And keep in mind that this tag itself is simply a prefix to the URL plus http://thishost[:thisport]/, and the override operation continues. Typically, you will want to stop the rewrite and redirect immediately, and you'll also need to use the ' L ' tag.

' Forbidden| F ' (force URL to prohibited forbidden)

Forces the current URL to be prohibited, that is, immediately feedback an HTTP response code of 403 (forbidden). With this tag, you can link several rewriteconds to conditionally block certain URLs.

' Gone| G ' (force URL to obsolete gone)

Forces the current URL to be obsolete, that is, to immediately feedback an HTTP response code of 410 (deprecated). Using this tag, you can indicate that the page has been discarded and no longer exists.

' Proxy| P ' (forced proxy proxy)

This tag causes the substitution component to be internally coerced to the proxy request, and immediately (that is, an immediate interruption of the rewrite rule processing) transfers the processing to the agent module. You must make sure that this replacement string is a valid URI, such as a common http://hostname, that can be processed for the Apache proxy module. With this tag, some remote components can be mapped to the local server namespace, thereby enhancing the functionality of the Proxypass directive. Note: To use this feature, the agent module must be compiled in the Apache server. If you are unsure, you can check the output of "httpd-l" for mod_proxy.c. If so, mod_rewrite can use this feature, and if not, you must enable Mod_proxy and recompile the HTTPD program.

' Last| L ' (Final Rule last)

Stops the rewrite immediately and no longer applies other overriding rules. It corresponds to the last command in Perl or the break command in the C language. This tag prevents the currently overridden URL from being overridden by its successor rule. For example, use it to rewrite the URL of the root path ('/') for the URL that actually exists, such as '/e/www/'.

' Next| N ' (re-executing next round)

Re-perform the rewrite operation (starting from the first rule). The URL that is processed again is not the original URL, but the URL that is processed by the last overriding rule. It corresponds to the next command in Perl or the Continue command in the C language. This tag can restart the rewrite operation, that is, immediately return to the loop's head. But be careful not to make a dead loop!

' Chain| C ' (linked to the next rule chained)

This tag causes the current rule to be linked to the next rule, which itself can be linked to and so repeated by its successor rule. It has the effect that if a rule is matched, it usually continues to process its successor, that is, the tag does not work, and if the rule cannot be matched, then the subsequent linked rule is ignored. For example, when performing an external redirect, you may need to delete ". www" (". www") on a directory-level rule set.

' Type| T=mime-type ' (force MIME type types)

Force the MIME type of the target file to be mime-type. For example, it can be used to simulate the scriptalias instruction in Mod_alias to internally force the MIME type of all files in the mapped directory to "application/x-httpd-cgi".

' Nosubreq| NS ' (for not handling internal child requests no internal sub-request)

This tag forces the override engine to skip the overriding rule when the current request is an internal child request. For example, when Mod_include tries to search for a possible directory default file (INDEX.XXX), Apache produces a child request internally. A child request, which is not necessarily useful, and may even raise an error if the entire rule set works. Therefore, you can use this tag to exclude certain rules. According to your needs, follow these guidelines: If you use a URL prefix with a CGI script to force them to be processed by a CGI script, and the error rate (or overhead) of the child request processing is high, you can use this tag in this case.

' Nocase| NC ' (ignores case No.)

It causes pattern to ignore the case that, when pattern matches the current URL, there is no difference between ' A-Z ' and ' A-Z '.

' Qsappend| QSA ' (Append request string query string append)

This tag forces the override engine to append a request string to an existing replacement string instead of a simple replacement. You can use this tag if you need to add information to the request string by overriding the rule.

' Noescape| NE ' (no URI is escaped from the output URI escaping)

This tag prevents mod_rewrite from applying a regular URI escape rule to the overridden results. In general, special characters (such as '% ', ' $ ', '; ') And so on) will be escaped to the equivalent hexadecimal encoding. This tag can block such escapes to allow symbols such as hundred semicolons to appear in the output, such as: rewriterule/foo/(. *)/bar?arg=p1\%3d$1 [R,ne]
Can make '/foo/zed ' turn to a secure request '/bar?arg=p1=zed '.

' Passthrough| PT ' (handed over to the next processor pass through)

This tag forces the override engine to set the URI field in the internal structure Request_rec to the value of the FileName field, which is just a small modification that enables the Alias,scriptalias from other URIs to the filename translator Redirect The output of the instruction is subsequently processed. Give an example of what it means: if you want to rewrite/ABC as/def through the mod_rewrite rewrite engine, and then convert/def to/ghi through Mod_alias, you can do this: Rewriterule ^/abc (. *)/def$1 [PT]
Alias/def/ghi
If the PT tag is omitted, although the mod_rewrite works correctly, that is, as a URI to the filename translator using the API, it can rewrite the uri=/abc/... For filename=/def/..., however, subsequent mod_alias will fail when attempting to translate the URI to the filename.
Note: You must use this tag if you need to mix different modules that contain URIs to the filename translator. Mixed use of Mod_alias and mod_rewrite is a typical example.

For Apache hackers
If the current Apache API is in addition to the URI to the filename hook, there is a hook with a filename to the filename, you do not need this tag! However, if there is no such hook, this tag is the only solution. The Apache group discussed this issue and added a hook to the Apache 2.0 version.

' skip| S=num ' (Skip the successor rule skip)

This tag forces the override engine to skip the NUM rule subsequent to the current matching rule. It enables the construction of a pseudo if-then-else: The last rule is a then clause, and the skipped Skip=n rule is an else clause. (IT and ' chain| C ' Mark is different!

' Env| E=var:val ' (Set environment variable environment variable)

This tag causes the value of the environment variable VAR to be Val, and Val can contain the regular expression $n and%n, which are extensible, reverse-referenced. This tag can be used more than once to set multiple variables. These variables can be referenced indirectly in many subsequent cases, but are usually referred to in Xssi (via or CGI (such as $ENV {' VAR '}) and by%{env:var} in the pattern of subsequent rewritecond directives. Use it to detach from the URL and remember some information.

' cookie|co=name:val:domain[:lifetime[:p Ath]] ' (set cookies)

It sets a cookie on the client browser. The name of the cookie is name and its value is Val. The Domain field is the field for the cookie, such as '. Apache.org ', where the optional lifetime is the number of minutes of the cookie's lifetime, and the path to the cookie is the alternative path.

Deep reading: http://oss.org.cn/man/newsoft/ApacheManual/mod/mod_rewrite.html

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.