Apache URL rewrite rewriterule parameters in detail

Source: Internet
Author: User

Apache URL rewrite rewriterule parameters in detail

We often do some rewriting of some URLs over Apache, so there are a lot of options for rewriting strategies, such as how to redirect, how to handle parameters, the order in which rules are matched.

For example, one of our apps will have the following URL rewrite rule: The following is the URL that needs to be redirected to http://page.test.com/channel/imall/index.html for domain imall.test.com access,

<virtualhost *>
ServerName imall.test.com#if ("${industry_port}"! = "+"): ${industry_port} #end

Rewriteengine on
Rewritecond%{http_host} =imall.test.com [NC]
Rewriterule ^/$ http://page.test.com/channel/imall/index.html [L,r]
</VirtualHost>

Before I wanted to write this, but found that the URL of the browser has changed, that is, R is an external redirect, then I hope the browser URL does not change, then the need for internal redirection or reverse proxy design. Only the official documents can be consulted at this time: http://httpd.apache.org/docs/2.2/rewrite/flags.html

A number of parameters are listed here: after each rule, you can add more than one parameter, each separated by commas

Rewriterule pattern Target [FLAG1,FLAG2,FLAG3]

B (Escape backreferences): Non-alphabetic characters are encoded,
For example, there is a URL of search.php?term=x & y/z, it is not set at this time the B parameter will be encoded as search.php?term=x%20&y%2fz=, is also wrong, then set the B parameter will be encoded AS/ SEARCH.PHP?TERM=X%20%26%20Y%2FZ so that the rules for URL rewrite can be correctly parsed and forwarded.

C|chain if it matches, it will continue to match the next rule, and if it does not match, skip all subsequent rules;

Co|cookie can set a cookie for the current URL, such as: [Co=name:value:domain:lifetime:path:secure:httponly]
Example:

Rewriteengine on
Rewriterule ^/index\.html-[co=frontdoor:yes:.example.com:1440:/]

Dpi|discardpathinfo Discard Path_info Information

E|ENV setting environment variables [E=var:val] [e=! VAR]
Example:

Rewriterule \. (png|gif|jpg)-[e=image:1]
Customlog Logs/access_log Combined Env=!image

F|forbidden directly returns 403 status code

Rewriterule \.exe-[F]

G|gone returns a 410 status code indicating that the resource is no longer available

Rewriterule oldproduct-[G,NC]

H|handler specifies that the request is handled by a handler, the following URL indicates that it is handled by the PHP processing engine

Rewriterule!\. -[h=application/x-httpd-php]

L|last matches, no longer matches the rules that follow.

Rewritebase/
Rewritecond%{request_uri}!=/index.php
Rewriterule ^ (. *)/index.php?req=$1 [l,pt]

N|next re-match starting with the first rule

Rewriterule (. *) A (. *) $1b$2 [N]

Nc|nocase Case insensitive

Rewriterule (. *\. ( Jpg|gif|png) $ http://images.example.com$1 [P,NC]

Ne|noescape does not transcode special characters, the default is to put & and other special characters into the 16 encoding,

Rewriterule ^/anchor/(. +)/bigpage.html#$1 [Ne,r]

Ns|nosubreq rules are not used on child requests, SSI (Server Side Include)

P|proxy as a reverse proxy forwarding request so that the browser URL will not change

Rewriterule/(. *) \. (jpg|gif|png) http://images.example.com/$1.$2 [P]

Pt|passthrough URL as a file path processing

Alias/icons/usr/local/apache/icons
rewriterule/pics/(. +) \.jpg/icons/$1.gif [PT]

Qsa|qsappend with the query parameter

rewriterule/pages/(. +)/page.php?page=$1 [QSA]

R|redirect redirect Default 302 redirect

S|skip skipping rule matches that you do not want to perform

# is the request for a non-existent file?
Rewritecond%{request_filename}!-f
Rewritecond%{request_filename}!-d
# If So, skip these, rewriterules
Rewriterule.? -[s=2]
Rewriterule (. *\.gif) images.php?$1
Rewriterule (. *\.html) docs.php?$1

T|type specifying the MIME type type

# Serve. pl files as plain text
Rewriterule \.pl$-[T=text/plain]

# Files with ' IMG ' in the name is JPG images.
Rewriterule IMG-[t=image/jpg]

Apache URL rewrite rewriterule parameters in detail

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.