Apache's rewrite rule learning

Source: Internet
Author: User

    1. 1, rewrite rules introduction:
      The main function of Rewirte is to implement a URL jump, and its regular expression is based on the Perl language. Can be based on server-level
      (httpd.conf) and directory-level (. htaccess) methods. If you want to use the rewrite module, you must first install or add
      Load the rewrite module. There are two ways to install the rewrite module directly when compiling Apache, and the other is to compile
      Apache installs Apache in DSO mode and then uses source code and APXS to install the rewrite module. Based on server-level
      (httpd.conf) has two methods, one is to use rewriteengine on to httpd.conf the global
      Open rewrite function, the other is to use rewriteengine on in the local rewrite function, the following example
      Note that you must use Rewriteengine on in each virtualhost to open the rewrite function. 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
      The. htaccess to declare rewriteengine on.

      2. Description of character meaning:

      Rewrite logo

      R[=code] (force redirect) forcing external redirection

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

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

      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 law

      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) coercion MIME type

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

      NC (no case) insensitive

      QSA (query string append) Append request string

      NE (no URI escaping of output) does not escape special characters in the output

      For example:

      rewriterule/foo/(. *)/bar?arg=p1\%3d$1 [R,ne] will correctly convert/foo/zoo to/bar?arg=p1=zed

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

      For example:

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

      Alias/def/ghi

      S=num (skip next rule (s)) skips num rule

      E=var:val (Set environment variable) setting environment variables

    1. 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

  1. 3. 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>

    In httpd-vhosts.conf, the following is the wording:

    Rewriterule ^/world/([\d]+) _ ([a-z]+). html$/world/index.php?c=world&a=housedetail&houseid={$1}& HOUSETYPE={$2} [L]

    Rewriterule ^/pinggu/([a-z]+)/([0-9]+). html/pinggu/index.php?c=pinggu&a=detail&city=$1&newcode=$2 &rewrite=1 [Nc,ne]

    Rewriterule ^/jiaju/([a-z]+)/jc_c ([\d]+). html/jiaju/index.php?c=jiaju&a=buildlist&city=$1&cid=$2

    Rewriterule ^/esf_bs/([a-z]*) _? (\d*) _?b? (\d*)/?m? ([0-9,]*) x? (\d*) A? ([0-9,]*) c? (\d*) p? ([0-9,]*) d? (\d*)/$/esf/index.php?purpose=%b1%f0%cafb&city=$1&district=$2&comarea=$3&price=$4&orderby= $5&area=$6&tags=$7&buildclass=$8&equipment=$9&rewrite

    Rewriterule ^/world/([\d]+) _ ([a-z]+). html$/world/index.php?c=world&a=housedetail&houseid={$1}& HOUSETYPE={$2} [L]

    #住宅rewrite规则
    rewriterule^/zf/([a-z]*) _? (\d*) _? (\d*)/?p? (\d*) H? (\d*) m? (\d*) c? (\d*) ([a-z0-9]*)/$ $8&&purpose=&city=$1&district=$2&comarea=$3&price=$4&room=$5& rtype=$6&tags=$7 [C,NC]
    Rewriterule^s? (\d*) r? (\d*) O? (\d*) F? (\d*) d? (\d*) p?x? (\d*) && (. *)/zf/index.php?$7&housetype=$1&railway=$2&towards=$3&floor=$4&equipment= $5&orderby=$6&rewrite [QSA,L,NC]

    #别墅rewrite规则
    Rewriterule ^/zf_bs/([a-z]*) _? (\d*) _? (\d*)/?p? (\d*) T? (\d*) m? (\d*) s? (\d*) ([a-z0-9]*)/?$ $8&&purpose=%b1%f0%ca%fb&city=$1&district=$2&comarea=$3&price=$4& buildclass=$5&rtype=$6&housetype=$7 [C,NC]
    Rewriterule ^h? (\d*) O? (\d*) F? (\d*) d? (\d*) p?x? (\d*) && (. *)/zf/index.php?$6&room=$1&towards=$2&floor=$3&equipment=$4&orderby=$5 &rewrite [QSA,L,NC]

    #写字楼rewrite规则
    Rewriterule ^/zf_app_xzl/([a-z]*) _? (\d*) _? (\d*)/?p? (\d*) A? (\d*) T? (\d*) c? (\d*) r? (\d*)/? /zf/index.php?purpose=%d0%b4%d7%d6%c2%a5&app_type=app&city=$1&district=$2&comarea=$3&price =$4&area=$5&propertysubtype=$6&tags=$7&&railway=$8&rewrite [QSA,L]

    #写字楼rewrite规则
    Rewriterule ^/zf_xzl/([a-z]*) _? (\d*) _? (\d*)/?p? (\d*) A? (\d*) T? (\d*) c? (\d*) r? (\d*)/? /zf/index.php?purpose=%d0%b4%d7%d6%c2%a5&city=$1&district=$2&comarea=$3&price=$4&area=$5 &propertysubtype=$6&tags=$7&&railway=$8&rewrite [Qsa,l]

    #写字楼rewrite规则
    Rewriterule ^/zf_xzl/([a-z]*) _? (\d*) _? (\d*)/?p? (\d*) A? (\d*) T? (\d*) c? (\d*) r? (\d*)/? /zf/index.php?purpose=%d0%b4%d7%d6%c2%a5&city=$1&district=$2&comarea=$3&price=$4&area=$5 &propertysubtype=$6&tags=$7&&railway=$8&rewrite [Qsa,l]
    Rewriterule ^/zf/([a-z]+) _XM ([0-9]+]/$/zf/index.php?c=zf&a=index&city=$1&projcodes=$2&src= Xiaoqu&rewrite [L]

Rewrite rule learning for Apache

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.