Apache Rewrite Redirection problem Rollup

Source: Internet
Author: User

Apache's rewrite module has been in use for a while. Each time is the first from the history of data changes. Today to make some time to tidy up and share with you, in fact, rewrite rules, I did not fully understand, in practice I used some of the listed, I believe that the general application of friends enough

Why you need to use rewrite rules.


Others Summary:

A Web site, if it is a long-term need to provide services on the Internet, there will be constant updates and maintenance, such as temporary transfer to other servers for maintenance, the organization of the directory structure, transform URLs and even change to the new domain name, etc., and in order to let the customer will not be affected by any, The best way to do this is to use Apache Rewrite rule (overriding
Rules).

When you enter an invalid parameter in the address bar of the browser, a database error message appears, which is a security risk

Search engines can't include all of your pages

The link address of a Web page is a series of parameters that are difficult to understand for browsing users and search engines

My Summary:

Rewrite can maintain the fixed stability of the interface, prevent the picture hotlinking, increase the search engine to climb the opportunity, the colleague also can give people unfathomable feeling (can't guess through what you use the backend is what technique and language writes)

The Common DZ Forum likes doing this, optimizes the search engine, has the seemingly static speech to give the person a kind of profound feeling:

Rewriterule ^ (. *)/archiver/((Fid|tid)-[/w/-]+/.html) $ $1/archiver/index.php?$2
Rewriterule ^ (. *)/forum-([0-9]+)-([0-9]+)/.html$ $1/forumdisplay.php?fid=$2&page=$3

Configuration

I am here with the Apache2 module, IIS can also be configured, I believe the principle is basically similar.

Define LoadModule rewrite_module modules/mod_rewrite.so in httpd.conf

A specific rewrite rule is defined in the virtual host configuration.

I'm sure everyone is familiar with this, so I won't tell you.

the Rewriterule rules for Apache are described in detail:

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

F (Force URL to is forbidden) disables the URL and returns a 403HTTP status code.

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

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

L (last rule) indicates that the current rule is the final one and stops parsing the rule after it is overridden.
N (Next round) to run the rewrite process again starting with the first rule.
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.

T=mime-type (Force MIME type) force MIME type
NS (Used only if no internal sub-request) is used only for not internal child requests

NC (no case) is case-insensitive
QSA (Query string append) Append request strings

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

For example: rewriterule/foo/(. *)/bar?arg=p1/%3d$1 [R,ne] will be able to correctly convert/foo/zoo to/bar?arg=p1=zed +3a? ' Z
PT (passes through to next handler) passed to the next processing

Example:: 5/UH/SX
Rewriterule ^/abc (. *)/def$1 [PT] # will be given to/def rule processing

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

E=var:val (SET environment variable) set environment variable

we get some practical examples from the actual combat:

Simple Virtual Host configuration:

<virtualhost *:80>
DocumentRoot E:/java_workspace/bsei_mapenjoy_passport_tomcat/webroot
ServerName www.boshilian.com
ErrorDocument 404/index.html
<ifmodule mod_rewrite.c>
Rewriteengine on
Rewriterule/22.htm (. +) $/help.jsp?$1 [L]
Rewriterule/33.htm (. +) $/help.jsp?$1 [R]
Rewriterule/44.htm (. +) $ http://www.sohu.com?$1 [L]
Rewriterule/55.htm (. +) $ http://www.sohu.com?$1 [R]

Rewriterule/66.htm (. +) $ http://www.sohu.com?$1 [P]

Rewritecond%{query_string} ^t/= (. +)? $ [NC]
Rewriterule ^/api$ http://www.sina.com.cn?t=%1 [P]

</IfModule>
</VirtualHost>

The simplest redirect:

The following list of commonly used simple, I believe you are familiar with, I will simply list:

URL does not change, direct orientation, similar to ForWord in Java:

Rewriterule/22.htm (. +) $/help.jsp?$1 [L]

URL changes, direct orientation, redirect steering, (support Cross-domain):
Rewriterule/33.htm (. +) $/help.jsp?$1 [R]

Rewriterule/44.htm (. +) $ http://www.sohu.com?$1 [L]
Rewriterule/55.htm (. +) $ http://www.sohu.com?$1 [R]

problem Rollup (here's the key)

Generally encountered 2 kinds of problems mostly, the following statements to solve:

Apache+tomcat Rewrite encountered problems, but also cross-domain, but also URL invariant redirect

Here I encountered a problem, that is, I use Apache + tomcat configuration integration, Apche for Tomcat redirect directory resolution is incorrect, rewrite always think which directory is Apache. If you use a cross domain jump (full domain name), the URL changes. It's not cool.

We can solve this problem by: (So the URL in the browser is still 66.htm, but the content is already Sina)

rewriterule/66.htm$ http://www.sina.com.cn [P]

rewriterule/66.htm$ http://127.0.0.1:8080/sohu/sd.jsp [P]

Note: Here p is the agent mode forwarding, must use the URL full name, and to ensure that modproxy open, that is, the following httpd.conf in two sentences:

LoadModule Proxy_module modules/mod_proxy.so
LoadModule Proxy_http_module modules/mod_proxy_http.so

If you do not open, you will receive a 403 Forbidden page.

rewrite in the redirect. Question mark Parameter Problem

Rewrite the previous argument. You can't send it, you need to deal with it.

Will

Http://www.boshilian.com/api?t=2323&v=23232&nond=323we

Rewrite is:

Http://www.sina.com.cn?t=2323&v=23232&nod=323we

The configuration is written as follows: (URL does not change)

Rewritecond%{query_string} ^t/= (. +)? $ [NC]
Rewriterule ^/api$ http://www.sina.com.cn?t=%1 [P]

or (URL change)

Rewritecond%{query_string} ^t/= (. +)? $ [NC]
Rewriterule ^/api$ http://www.sina.com.cn?t=%1 [L]

So we can put. The following T-parameter forwards the past.

The Official document explains:
Note: query string
Pattern does not match according to the query string. To achieve this, you must use a rewritecond instruction with%{query_string} variables.

Of course, you can also create a URL that contains a query string in the replacement string: Use a question mark in the replacement string to indicate that the part that follows should be injected back into the query_string.

To delete an existing request string, you can use a question mark to terminate the replacement string. To combine the old and new query strings, use the [QSA] flag.

Write so much first, rewrite also have a lot of parameters combination of applications, I do not introduce here, application in-depth this can be a message to discuss.

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.