Introduction to rewrite rules in Apache

Source: Internet
Author: User

There are two methods for server-level (httpd.conf), one is to use rewriteengine on to open the rewrite function under the global httpd.conf, and the other is to use Rewriteengine in the local area. On to turn on the rewrite feature, the following will illustrate the need to note that the rewrite feature must be turned on with Rewriteengine on in each virtualhost. Otherwise, there is no rewriteengine on the VirtualHost and the rules in it will not take effect.

Based on the directory level (. htaccess), it is important to note that the FollowSymLinks property of this directory must be opened and in. htaccess to declare rewriteengine on.

Actual combat

Example:

The code is as follows Copy Code
Rewriteengine on
Rewritecond%{http_user_agent} ^msie [Nc,or]
Rewritecond%{http_user_agent} ^opera [NC]
Rewriterule ^.*-[f,l] here "-" means no replacement, browsers for IE and opera visitors will be barred from accessing.


Example:

The code is as follows Copy Code
Rewriteengine on
Rewritebase/test
Rewritecond%{request_filename}.php-f
Rewriterule ([^/]+) $/test/$1.php
#for Example:/test/admin =>/test/admin.php
Rewriterule ([^/]+). html$/test/$1.php [L]
#for Example:/test/admin.html =>/test/admin.php

Configure a multiuser virtual server with the Apache URL rewrite

To implement this feature, you first need to open the domain name resolution on the DNS server (do it yourself or find a domain Name Service provider). For example, I have *.kiya.us and *.kiya.cn all resolved to my IP address 70.40.213.183.

Then, take a look at the setup of my Apache virtual host for *.kiya.us.

  code is as follows copy code

ServerAdmin webmaster@kiya.us
documentroot/home/www/111cn.net
ServerName dns.kiya.us
Serveralias dns.kiya.us kiya.us *.kiya.us
Customlog/var/log/httpd/osa/access_log.log "Common
Errorlog/var/log/httpd/osa/error_log.log"
allowoverride None
Deny,allow

#AddDefaultCharset GB2312

Rewriteengine on
Rewritecond%{ Http_host} ^[^.] +.kiya. (cn|us) $
Rewriterule ^ (. +)%{http_host}$1 [C]
Rewriterule ^ ([^.] +). Kiya. (Cn|us) (. *) $/home/www/111cn.net/sylvan$3?un=$1&%{query_string} [L]

In this setting, I set the document root of *.kiya.cn and *.kiya.us to/home/www/111cn.net


Restricted directories can only display pictures

The code is as follows Copy Code

< Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{request_filename}!^.*. (gif|jpg|jpeg|png|swf) $
Rewriterule. *$-[f,l]
</ifmodule>

Description of the Rewriterule rule expression:
. Match any single word character
[chars] matching string: chars
[^chars] does not match string: chars
TEXT1|TEXT2 selectable string: Text1 or Text2
? Match 0 to 1 characters
* Match 0 to more characters
+ match 1 to more characters
^ String Start flag
$ string End Flag
N Escape Character Flag

Reverse reference $N variable invocation for matching in rewriterule (0 <= N <= 9)
Reverse reference%N for the last matching variable call in Rewritecond (1 <= N <= 9)

Rewritecond the applicable identifier
' Nocase| NC ' (no case) ignores size
' Ornext| or ' (or next condition) logic or, you can match multiple rewritecond conditions at the same time

Rewriterule the applicable identifier

' Redirect| R [=code] ' (Force redirect) forced overrides to an external turn based on HTTP (note URL changes) such as: [R=301,l]
' Forbidden| F ' (Force URL to is forbidden) overridden to prohibit access
' Proxy| P ' (force proxy) overrides the HTTP path that is accessed through the proxy
' Last| L ' (last rule) The final rewrite rule flag, if matched, no longer executes subsequent rules
' Next| N ' (next round) loops the same rule until the match is not met
' Chain| C ' (chained with next rule) if the rule is matched, continue with the following rules with the chain flag.
' Type| T=mime-type ' (Force MIME type) specifies MIME type
' Nosubreq| NS ' (used only if no internal sub-request) skipped if it is an internal child request
' Nocase| NC ' (no case) ignores size
' Qsappend| QSA ' (query string append) append query strings
' Noescape| NE ' (no URI escaping of output) prevents characters in a URL from automatically escaping into a%[0-9]+ form.
' Passthrough| PT ' (pass through to next handler) applies the rewrite results to Mod_alias
' Skip| S=num ' (skip next rule (s)) skip the following few rules
' Env| E=var:val ' (Set environment variable) add environment variables

List of flags for Apache mod_rewrite rules rewriting

1) R[=code] (Force redirect) force external redirection
Forces a URL to be redirected to an external string, plus the http://thishost[:thisport]/prefix. If code is not specified, the default 302 HTTP status code is used.
2 F (force URL to is forbidden) disables the URL and returns a 403HTTP status code.
3 G (force URL to is gone) force URL to gone, return 410HTTP status code.
4) P (force proxy) enforces the use of proxy forwarding.
5 L (last rule) indicates that the current rules are the final rule, stopping the parsing of the rules after the analysis.
6) N (next round) again runs the rewrite process from the first rule.
7) C (chained with next) is associated with the next rule

If the rule match is handled normally, the flag is invalid and if it does not match, all of the following associated rules are skipped.

8) T=mime-type (Force MIME type) enforces MIME types
9) NS (used only if no internal sub-request) is used only for not internal child request
Ten) NC (no case Case-insensitive
One) QSA (query string append) Append request string
-NE (no URI escaping of output) not in output escape special characters
For example: Rewriterule/foo /(. *)/bar?arg=p1%3d$1 [R,ne] will be able to correctly convert/foo/zoo to/bar?arg=p1=zoo
) PT (passes through to next handler) for the next processing
For example: br> rewriterule ^/abc (. *)/def$1 [PT] # will be given to the/def rule processing
Alias/def/ghi
) s=num (skip next rule (S) skips the NUM Bar rules
15) E=var:val (Set environment variable) sets the environment variable

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.