Apache's rewrite rules are detailed

Source: Internet
Author: User

Rewrite is a kind of server rewrite pulse technology, it can make the server can support URL rewriting, is a kind of latest popular server technology. It also enables the ability to restrict access to a site by a specific IP.

Tools/Materials
    • Apache

Method/Step
  1.  

    rewrite flag

    R[=code] (Force redirect) Force external redirection

    G (force URL to is gone) forces the URL to gone and returns the 410HTTP status code. The

    P (Force proxy) enforces the use of proxy forwarding. The

    L (last rule) indicates that the current rule is the final rule, stopping the rewrite of the rule after parsing.

    N (next round) re-runs the rewrite process starting with the first rule.

    C (chained with next rule) is associated with the next rules

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

    T=mime-type (Force MIME Type) force MIME-type

    ns  (used only if no internal sub-request) is used only for internal sub-requests

    NC (no case) is insensitive

    QSA (query String append) Append request string

    NE (no URI escaping of output) does not 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=zed

    PT (pass through to next handler) passed to the next processing

    For example:

    Rewriterule ^/abc (. *)/def$1 [PT] # will be handed to/def rule processing

    Alias/def/ghi

    S=num (skip next rule (S)) skip Num Bar rules

    E= Var:val (Set environment variable) set environment variable

  2.  

    rewritecond marker

    ' nocase| NC ' (no case) ignores size

    ' ornext|or ' (or Next condition) logic or, can match multiple rewritecond conditions rewriterule applicable marker

    ' redirect| R [=code] ' (Force redirect) forced override for external steering based on HTTP (note URL changes) such as: [R=301,l]

    ' forbidden| F ' (Force URL to is forbidden) is overridden to prohibit access to

    ' proxy| P ' (force proxy) overrides the HTTP path that is accessed through the proxy

    ' last| The final rewrite rule flag of L ' (last rule), if matched, no longer executes subsequent rules

    ' next| N ' (next round) loops the same rule until the matching

    ' chain| is not met 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 the MIME type

    ' nosubreq| NS ' (used only if no internal sub-request) skips

    ' nocase| if it is an internal sub-request NC ' (no case) ignores size

    ' qsappend| QSA ' (query string append) appends the query string

    ' Noescape|ne ' (no URI escaping of output) to prevent characters in the URL from automatically escaping into%[0-9]+ form.

    ' passthrough|pt ' (pass through to next handler) applies the rewrite result to the Mod_alias

    ' skip| S=num ' (skip next rule (s)) skips the following few rules

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

  3. Rewrite Server variables:

    HTTP headers:http_user_agent, Http_referer, Http_cookie, Http_host, http_accept

    Connection & Request:remote_addr, query_string

    Server Internals:document_root, Server_port, Server_protocol

    System Stuff:time_year, Time_mon, Time_day

  4. Description of the rewrite rule expression:

    . Match any single character

    [chars] Match string: chars

    [^chars] mismatch 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 used for matching variable calls in Rewriterule (0 <= N <= 9)

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

  5. actual operation: example: Rewriteengine on

    Rewritecond%{http_user_agent} ^msie [Nc,or]

    Rewritecond%{http_user_agent} ^opera [NC]

    Rewriterule ^.*-[f,l] here "-" means no replacement, the browser for IE and opera visitors will be banned from access.

    Example:

    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

    Restrict a catalog to display only pictures

    < Ifmodule mod_rewrite.c>

    Rewriteengine on

    Rewritecond%{request_filename}!^.*\. (gif|jpg|jpeg|png|swf) $

    Rewriterule. *$-[f,l]

    </ifmodule>

Apache's rewrite rules are detailed

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.