Rewrite URL redirection is used to redirect URLs and hide real addresses. It is based on Perl-based regular expressions. It usually helps us achieve quasi-static, quasi-directory, domain name jump, and prevent leeching.
1. Main Functions of Apache rewrite
It is to achieve URL jump and hide real addresses, based on the Perl Regular Expression specification. It usually helps us achieve quasi-static, quasi-directory, domain name jump, and prevent leeching.
2. Apache rewrite Configuration
There are two types of rewrite configurations in Apache. One is for the configuration of the entire Apache server. The rewrite rules for this configuration are directly written under httpd. conf. The configuration steps are as follows:
(1) remove the "# loadmodule rewrite_module modules/mod_rewrite.so" # "in the httpd. conf file;
(2) then write the following rules in httpd. conf:
Rewriteengine on
When starting a file ending with ".html" with "_", "$1" is replaced with a character that matches ". *", and the corresponding "test. php" page is accessed.
Rewriterule ^/T _ (. * example .html $/test. php? Id = $1
The other is the configuration of a directory under the Apache server. The rewrite rules of this configuration need to be written by creating a. htaccess file under this directory. The configuration steps are as follows:
(1) remove the "# loadmodule rewrite_module modules/mod_rewrite.so" # "in the httpd. conf file;
(2) modify "AllowOverride none" to "AllowOverride all" in the httpd. conf file, and set options to "all". Otherwise, problems may occur.
(3) create a. htaccess file in the directory, open it in notepad, and write the following rules:
Rewriteengine on
Rewriterule ^/T _ (. * example .html $/test. php? Id = $1
3. Writing Apache Rewrite Rules
Rewriteengine on
Rewriterule ^/test ([0-9] * example .html $/test. php? Id = $1
Rewriterule ^/New ([0-9] *)/$/New. php? Id = $1 [R]
Rewriteengine on
# When the address we access is not starting with www.163.com, execute the next rule.
Rewritecond % {http_host }! ^ Www.163.com [Nc]
Rewriterule ^/(. *) http://www.163.com/[l]
4. Apache rewrite rule Modifier
1) r forces external redirection
2) f disables the URL and returns the 403http status code.
3) If G forces the URL to be gone, the response HTTP status code is returned.
4) P enforces proxy forwarding.
5) L indicates that the current rule is the last rule, and the rule is overwritten after analysis is stopped.
6) n re-run the rewrite process from the first rule.
7) C. associate with the next rule
If the rule matches, it is processed normally. The following modifier is invalid.
8) t = mime-type (Force MIME type) force MIME type
9) ns is only used for non-Internal subrequests
10) the NC is case insensitive.
11) QSA append request string
12) NE does not output escape special characters \ % 3d $1 is equivalent to = $1
Simple configuration of Apache rewrite URL redirection