Apache's mod_rewrite learning (rewriterule rewrite rule syntax) goes

Source: Internet
Author: User

Rewriterule
Syntax:rewriterule Pattern Substitution [flags]
A rewriterule directive, which defines a rewrite rule, is important for the order of the rules. For Apache1.2 and later versions, the template (pattern) is a POSIX regular that matches the current URL. The current URL is not necessarily the URL that was originally submitted, because the URL might have been processed by some rules before this rule.
For Mod_rewrite,! is a valid template prefix that means "non", which is handy for describing "not satisfying a certain match condition", or as the last default rule. When used! , you cannot have a grouped wildcard character in the template, and you cannot do a back reference.
When the match succeeds, the substitution is used to replace the corresponding match, which, in addition to being a normal string, can include:
1. $N, reference the matching string in Rewriterule template, N for ordinal, n=0..9
2.%N, referencing the matching data in the last Rewritecond template, N denotes the ordinal
3.%{varname}, Server variables
4. ${mapname:key|default}, Map function call
The extensions of these special content are carried out in the order described above.
All related parts of a URL are replaced by substitution, and the replacement process continues until all rules are executed, unless you explicitly interrupt the process with the L flag.
When Susbstitution has a "-" prefix, it means that no substitution is made and only matching checks are done.
With Rewriterule, you can define a URL with a request string (Query string) and simply add one to the sustitution. , which means that the content thereafter is placed in the QUERY_STRING variable. If you want to empty a query_string variable, just use the? End the substitution string.
If you add a http://thishost[:p ort] prefix to a substitution, mod_rewrite will automatically remove the suffix. Therefore, using http://thisthost to do an unconditional redirect to yourself will be difficult to work with. To achieve this effect, you must use the R flag.
Flags is an optional parameter that is separated by commas when multiple flags appear at the same time.
1. ' Redirect| R [=code] ' (forced redirection)
Adds a prefix of http://thishost[:thisport]/to the current URI, generating a new URL that forces an external redirect (external redirection, which is sent to the client by the generated URL, The request is made again by the client with a new URL, although the new URL still points to the current server. If no code value is specified, the HTTP answer takes the Status value 302 (MOVED temporarily), and if you want to use a value other than 300-400 (excluding 400), you can specify it by the corresponding number in the code location, or you can specify it with the flag name: Temp (default value) , permanent, seeother.
Note that when using this flag, it is true that the substitution is a valid URL, which only adds the http://thishost[:thisport]/prefix before the URL, and the rewrite operation continues. If you want to redirect the new URL immediately, use the L flag to rewrite the process.
2. ' Forbidden| F ' (Force access to the resource referred to by the URL)
Returns the reply package for the status value 403 (FORBIDDEN) immediately. Use this flag in conjunction with the appropriate rewriteconds to block access to certain URLs.
3. ' Gone| G ' (Force return URL refers to a resource that does not exist (gone))
Returns the reply package for the status Value 410 (GONE) immediately. Use this flag to mark the URLs that the resources refer to are permanently gone.
4. # ' proxy| P ' (force the current URL to be sent to the proxy module)
This flag, forcing substitution as a request to the proxy module, and immediately will be sent to the proxy module. Therefore, you must ensure that the substitution string is a valid URI (for example, the typical case starts with http://hostname), or you will get an error from the proxy module. This flag is a more robust implementation of the PROXYPASS directive, which maps remote requests (remotes stuff) to the local server's namespace (namespace).
Note that this feature must be used to ensure that the proxy module has been compiled into the Apache server program. You can use the "httpd-l" command to check if the output contains mod_proxy.c to confirm. If you do not, and you need to use this feature, you will need to recompile the ' httpd ' program and use Mod_proxy to be valid.
5. ' Last| L ' (Last rule)
Aborts the rewrite process and no more rewrite rules are applied to the current URL. This corresponds to the last command of Perl or the break command of C.
6. ' Next| N ' (Next round)
Re-start the rewrite process from the first rewrite rule, and the URL in the new procedure should not be the same as the original URL. This corresponds to Perl's next command or C's continue command. Be careful not to create a dead loop.
7. # ' chain| C ' (crawl binding of the current rule to its successor rules (chained))
When a rule matches, the process is the same as no crawl binding, and if the rules do not match, then the subsequent rules crawl tied together are not checked and executed.
8. ' Type| T=mime-type ' (Force MIME type)
Forces the mime-type of the destination file to be a MIME type. For example, this can be used to mimic the scriptalias designation of a directory by the Mod_alias module, by forcing the type of all files in that directory to be changed to "application/x-httpd-cgi".
9. ' Nosubreq| NS ' (used only if no internal sub-request)
This flag forces the rewrite engine to skip the rewrite rules for internal sub-request. For example, when Mod_include tries to find a default file under a directory (INDEX.XXX), sub-requests occurs inside Apache. Sub-requests is not always useful, and in some cases an error occurs if the entire ruleset is applied to it. Use this flag to exclude the execution of some rules.
Ten. ' Nocase| NC ' (template is case insensitive)
This flag causes the template to match the current URL with a case-insensitive difference.
One. ' Qsappend| QSA ' (Append request string (query string))
This flag, forcing the rewrite engine to append a portion of the string to the substitution request string, is not replaced by the original. With this flag, you can use a rewrite rule to add more data to the request string.
' Noescape|ne ' (do not escape special characters in the output result)
Typically, in the output of Mod_write, special characters (such as '% ', ' $ ', '; ', etc.) are escaped to their 16-binary form (for example, '%25 ', '%24 ', and '%3b '). This flag prevents Mod_rewrite from doing such operations on the output. This flag can only be used in Apache 1.3.20 and later versions.
' Passthrough|pt ' (via next processor)
This flag forces the rewrite engine to replace the value of the URI field in the internal REQUEST_REC data structure with the value of the filename field: Using this flag, you can make subsequent other uri-to-filename converter alias, Scriptalias, redirect and other instructions, but also can normally handle the output of rewriterule instructions. Use a small example to illustrate its semantics: if you want to convert/ABC to/def with the mod_rewrite rewrite engine, and then use Mod_alas to rewrite/def as Ghi, you would:
Rewriterule ^/abc (. *)/def$1 [PT]
Alias/def/ghi
If the PT flag is ignored, then mod_rewrite can also do a good job, if., Will uri=/abc/... Convert to filename=/def/..., fully conforms to the action of a uri-to-filename converter. Next Mod_alias try to do uri-to-filename conversion will be a problem.
Note: If you want to mix instructions for different modules that contain url-to-filename converters, you must use this flag. The most typical examples are the use of Mod_alias and mod_rewrite.
' Skip| S=num ' (Skip the following NUM rules)
When the current rule matches, force the rewrite engine to skip the subsequent NUM rules. Use this to mimic the IF-THEN-ELSE structure: the last rule flag for the then clause is skip=n, and N is the number of rule bars for the ELSE clause.
"Env| E=var:val ' (Setting environment variables)
Set the value of the environment variable named var to Val, where Val can contain a regular back reference ($N or%n). This flag can be used multiple times to set multiple environment variables. The variables set here can be referenced in a variety of situations, such as in Xssi or CGI. Alternatively, it can be referenced in the form of%{env:var} in the Rewritecond template.
16.

Note: Be sure not to forget that in a server-wide configuration file, the template (pattern) is used to match the entire URL, whereas in a directory-scoped configuration file, the directory prefix is always automatically removed and then matched by the template, which is automatically added when the replacement is complete. This feature is important for many kinds of overrides, because if you do not go to the prefix, the parent directory is matched, and the parent directory information is not always available. One exception is that when there is an http://In the substitution, the prefix is no longer automatically incremented, and if the P flag appears, it is forced to move to the proxy.
Note: If you want to start the rewrite engine within a directory scope, you need to set "Rewriteengine on" in the appropriate directory configuration file, and the "Options followsymlinks" of the directory must be set. If the administrator does not open followsymlinks for security reasons, you cannot use the rewrite engine.

--------------------------------------------------------------------------------------------
Apache-Modules-Mod_rewrite-rewritecond-Avoid static picture hotlinking by checking http_referer to have a serious impact on performance
Common applications for Apache rewrite rules (rewrite)

One: Purpose

This article is intended to provide an approach to how to use Apache rewrite rules to resolve some common URL rewriting methods, through common
Examples give users some basic methods and clues for using rewrite rules.

Two: Why do I need to use rewrite rules?
A Web site, if it is a long-term need to be placed on the Internet to provide services, there is bound to be constantly updated and maintained, such as pro
to other servers for maintenance, reorganize the directory structure, transform URLs and even change to new domain names, etc.
In order for customers to be unaffected by this, the best way is to use Apache Rewrite Rule (overriding
Rules).

Three: The scope of the rewrite rule
1) can be used in Apache Master config file httpd.conf
2) can be used in the virtual host configuration defined in the httpd.conf
3) You can use the span profile in the base directory. htaccess

Four: Application conditions for rewrite rules
Only when a user's Web request is eventually directed to the Apache background of a Web server, the Web server accepts
Incoming request, depending on the profile, whether the request is a master configuration or a virtual host, based on the user's request in the browser
URI to pair the rewrite rule and pair it according to the actual request path. Rewrite rules in htaccess. Finally put the request
Content is returned to the user, there are two possible types of responses:

1) external redirection (Redirect) of the content of the browser request to another URL.
Ask the browser to make a request again with a new Uri (r=301 or r=302, temporary or permanent redirect)
such as: A site has a formal URL and alias URL, the alias URL to redirect to the regular URL, or the site change
into a new domain.
REDIRECT the old domain name to the new domain name (Redirect)

2) may also be sent back to the customer by the Apache internal Sub-request agent to generate new content [P,l]
This is Apache internally according to the rewritten URI inside the request content through the proxy module and send back the content to the customer, while the customer
-Side browser and
It is not known that the URI in the browser will not be overridden. But the actual content is obtained by Apache based on the URI of the rewrite rule.
such as: Apache running on the corporate firewall to start this proxy rewrite rule, agent on the internal network segment of the Web services
The request of the device.

Five: How does the rewrite rule work?
We assume that you have compiled mod_rewrite into a module when compiling Apache, and that you are sure that your httpd.conf has
LoadModule Rewrite_module libexec/mod_rewrite.so
And there are addmodule in the
Addmodule mod_rewrite.c
You can use rewrite rules.
When an external request comes to Apache,apache call the definition in the rewrite rule to override the request specified by the user's browser
URI, the last overridden URI is sent by the browser for another request if it is a redirect, and if the proxy overrides
After the URI to the proxy module to request the final content (contents), and finally send back to the browser.

Six: When to use the rewrite rule definition in. htaccess?
If you do not have administrator rights on the server where your site content resides, or if your site is placed on the ISP's server
On hosting and so on, you can't overwrite the master profile, but you can make the directory where your Web site content resides
Have write permission, then you can set your own. htaccess
The document achieves the same purpose. But you need to make sure that the main profile in the directory where your site is located is defined in the following
Capacity:

Options Indexes FollowSymLinks
AllowOverride All

Otherwise, your. htaccess won't work.

Seven: Application examples
Assuming that Apache is compiled and installed under the/usr/local/apache/directory of the host 192.168.1.56, we compile
The rewrite and proxy modules are translated into the.

1) hide a directory under Apache so that any requests for that directory are redirected to another file.

The realization method of a> httpd.conf

Let's put the part of the face down to/usr/local/apache/conf/httpd.conf.


Options Indexes FollowSymLinks
AllowOverride All
Rewriteengine on
Rewritebase/
Rewriterule ^ (. *) $ index.html.en [r=301]


Note: Rewriteengine on is a rewrite engine switch, and if set to off, any rewrite rule definitions will not be
The other advantage of this switch is that if you want to remove the rewrite rules temporarily, then turn off and then restart Apache.
You do not have to comment out the rewrite rules for the following strips.
The Rewritebase/function is if the section is rewritten in the following Rewriterule definition (file
Name Index.html.en) in front of no/, is relative to the directory, relative to this rewritebase after the definition is also
is/usr/local/apache/htdocs/index.html.en, otherwise, if there is no rewritebase/this
One item, it is rewritten as
Http://192.168.1.56/usr/local/apache/htdocs/manual/index.html.en, apparently.
Not correct.

But here we can also not rewritebase/, but instead
Rewriteengine on
Rewriterule ^ (. *) $/index.html.en [r=301]
Or
Rewriteengine on
Rewriterule ^ (. *) $ http://192.168.1.56/index.html.en [r=301]

B>. How to implement Htaccess

Let's put the following part to Httpd.conf.


Options Indexes FollowSymLinks
AllowOverride All


and put the following part into the/usr/local/apache/htdocs/manual/.htaccess.
Rewriteengine on
Rewritebase/
Rewriterule ^ (. *) $ index.html.en [r=301]

Note: Any changes made to the file. htaccess do not require a restart of Apache.

Q: What if I redirect this manual directory to the user Jephe's own home directory?
Use the following. htaccess scenario.
Rewriteengine on
Rewritebase/~jephe/
Rewriterule ^ (. *) $ $ [r=301]

Requests for any files in the manual directory are redirected to the same file in the ~jephe directory.

2) Convert Www.username.domain.com's home page request for username
Www.domain.com/username

The request for http/1.1 includes a host:http header, which we can rewrite with the following rule set
Http://www.username.domain.com/anypath to/home/username/anypath

Rewriteengine on
Rewritecond%{http_host} ^www\. [^.] +\.host\.com$
Rewriterule ^ (. +)%{http_host}$1 [C]
Rewriterule ^www\. ([^.] +) \.host\.com (. *)/home/$1$2

Note:
Rewritecond conditional rewrite rules that apply the following rewrite rules when the conditions defined later are met.
Rewritecond have various variables
, please refer to the relevant documentation.

3) Rewrite rules on the firewall proxy requests for servers on the internal network segment.

Namevirtualhost 1.2.3.4


ServerName www.domain.com
Rewriteengine on
Proxyrequest on
Rewriterule ^/(. *) $ http://192.168.1.3/$1 [p,l]


Note: When an external browser requests www.domain.com to be resolved to the IP address 1.2.3.4, Apache surrenders
Mod_rewrite processing is converted into
Http://192.168.1.3/$1 after the agent module Mod_proxy get the content to be transferred back to the user's browser.


4) Basic pre-set conversion map table to rewrite Rewritemap

Convert Www.domain.com/{countrycode}/anypath to the URI specified in the map table, above the virtual host
The definition in

Rewritelog/usr/local/apache/logs/rewrite.log
Rewriteloglevel 9

Rewriteengine on
Proxyrequest on
Rewritemap Sitemap Txt:/usr/local/apache/conf/rewrite.map
Rewriterule ^/([^/]+) +/(. *) $ http://%{remote_host}::$1 [C]
Rewriterule (. *)::([a-z]+] $ ${sitemap:$2|http://h.i.j.k/} [r=301,l]

The contents of the file/usr/local/apache/conf/rewrite.map are as follows:

SG http://a.b.c.d/
SH http://e.f.g.h/

Note: When the user requests Http://www.domain.com/sg/anypath, it is rewritten as
Http://a.b.c.d/anypath.
When debugging is required, use Rewritelog and Rewriteloglevel 9 and 9 for maximum debug
Information
The minimum is 1, the minimum debugging information, the default is 0, no debugging information.
The syntax of a sitemap is ${sitemap:lookupkey | DefaultValue}, some books put $ into% is wrong
The wrong.

Apache's mod_rewrite learning (rewriterule rewrite rule syntax) goes

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.