Apache Rewrite Pseudo-static and URL jump configurations

Source: Internet
Author: User
Tags bind

The main function of Rewirte is to implement URL jump. The regular expression of the Rewirte code is based on the Perl language. server-level (httpd. conf) and directory (. htaccess), while Apache generally uses Directory-level (. htaccess) method, so Haitian's learning and recording here is (. htaccess) method, commonly known as pseudo static.

Since the main function of Rewirte is to implement URL jump, Haitian will directly start with URL jump!

In general, the pseudo-static redirect of. htaccess is written as (in the following example, when a domain name of a.111cn.net is input, it jumps to www.111cn.net ):

The code is as follows: Copy code


1 RewriteEngine on

2 RewriteCond % {HTTP_HOST} ^ a.111cn.net [NC]

3 RewriteRule ^ (. *) http://www.111cn.net/[L]

 
Mark of Apache Rewrite rule rewriting

The code is as follows: Copy code
1) R [= code] (force redirect) force external redirection
Force the replacement string to add http: // thishost [: thisport]/prefix to the external URL. If the code is not specified, the default 302 HTTP status code will be used.
 
2) F (force URL to be forbidden) disables the URL and returns a 403HTTP status code.
 
3) G (force URL to be gone) forces the URL to be GONE and returns the response HTTP status code.
 
4) P (force proxy) enforces the use of proxy forwarding.
 
5) L (last rule) indicates that the current rule is the last rule. After the analysis is stopped, the rule is overwritten.
 
6) N (next round) re-runs the rewrite process from the first rule.
 
7) C (chained with next rule) is associated with the next rule
If the rule matches, it is processed normally and the flag is invalid. If the rule does not match, all associated rules are skipped.
 
8) T = MIME-type (force MIME type) force MIME type
 
9) NS (used only if no internal sub-request) is used only for non-internal subrequests
 
10) NC (no case) is case insensitive
 
11) QSA (query string append) append request string
 
12) NE (no URI escaping of output) does not escape special characters
Example: RewriteRule/foo/(. *)/bar? Arg = P1 % 3d $1 [R, NE] will be able to correctly convert/foo/zoo to/bar? Arg = P1 = zoo
 
13) pass through to next handler to the next processing

 
For example:

The code is as follows: Copy code
RewriteRule ^/abc (. *)/def $1 [PT] # will be handed over to/def rules for processing
Alias/def/ghi
 
14) S = num (skip next rule (s) skipping num rules
 
15) E = VAR: VAL (set environment variable) set environment variables

The following is an example:

Example 1: Rewrite rules defined in a VM. function is to client request URL address prefix is not www.aaa.cn and 70.40.213.183 jump to the host prefix for http://www.aaa.cn. this avoids the same content of the web page with multiple pointed domain names, such as http://aaa.cn, is not conducive to SEO.

The code is as follows: Copy code

1 RewriteEngine on
# Enable the rewirte function

2 RewriteCond % {HTTP_HOST }! ^ Www.aaa.cn [NC]
# Declare that the prefix of the URL address requested by the Client is not www.kiya.cn. [NC] indicates that the case sensitivity is ignored.

3 RewriteCond % {HTTP_HOST }! ^ 70.40.213.183 [NC]
# Declare that the prefix of the Client request URL is not 70.40.213.183

4 RewriteCond % {HTTP_HOST }! ^ $
# Declare that the prefix of the host requested by the Client is not blank

5 RewriteRule ^ (. *) http://www.aaa.cn/[L]

# It means that if the prefix of the host requested by the Client meets the preceding conditions, the system will jump directly to http: // aaacn/, and [L] means that the rewrite operation is stopped immediately, other rewrite rules are no longer applied. here. * matching all URLs does not contain line breaks. The () bracket function is to mark all characters so that they can be used later. is to reference (. *) character.


Example 2: enable the new domain name www.111cn.net. the original domain name ss.aaa.cn and the address of the Forum ss.aaa.cn/bbs/need to be directed to the new domain name, so that the user can find it and make the original forum URL valid without 404 not found. for example, the original http://ss.aaa.cn/bbs/tread-60.html, so that it continues to be valid under the new domain name, click to forward to http://bbs.111cn.net/tread-60.htmland other web pages, such as the original http: // www.111cn.net/purchase.

The code is as follows: Copy code

1 RewriteEngine On

2 RewriteCond % {REQUEST_URI} ^/bbs/

3 RewriteRule ^ bbs/(. *) http://bbs.aaa.cn/#1 [R = permanent, L]

4 RewriteCond % {REQUEST_URI }! ^/Bbs/

5 RewriteRule ^ (. *) http://www.111cn.net/#1 [R = permanent, L]

 
Example 3: both of the following requirements are met:
1. Access http://www.111cn.net/xxx.php with http://www.111cn.net/xxx/
2. Access http://yyy.www.111cn.net with http://www.111cn.net/user.php? Username = yyy

 

The code is as follows: Copy code

View sourceprint?
1 RewriteEngine On

2 RewriteCond % {HTTP_HOST} ^ www.111cn.net

3 RewriteCond % {REQUEST_URI }! ^ User. php $

4 RewriteCond % {REQUEST_URI}. php $

5 RewriteRule (. *). php $ http://www.111cn.net/4101/ [R]

6 RewriteCond % {HTTP_HOST }! ^ Www.111cn.net

7 RewriteRule ^ (. +) % {HTTP_HOST} [C]

8 RewriteRule ^ ([^.] +) .www.111cn.net http://www.111cn.net/user.php? Username = $1


Example 4: Set the URL address/type. php? Typeid = * jump to/type *. html, URL address/type. php? Typeid = * & page = * jump to/type * page *. html

The code is as follows: Copy code

1 RewriteRule ^/type(%0-9%%%%%.html $/type. php? Typeid = $1 [PT]

2 RewriteRule ^/type(%0-9%%%%%%page(%0-9%%%%%%.html $/type. php? Typeid = $1 & page = $2 [PT]

I believe that through the above four simple examples, we can write simple Apache Rewrite URL redirection


This article introduces the basic rewrit knowledge of apache. Let's take a look at some examples.
 

The code is as follows: Copy code

RewriteEngine on
# Change yourdomain.com to the domain name you want to bind.
RewriteCond % {HTTP_HOST} ^ (www .)? Mq35.com $

# Change subfolder to the directory to be bound.
RewriteCond % {REQUEST_URI }! ^/Bbs/

# Do not change the following two lines.
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_FILENAME }! -D

# Change subfolder to the directory to be bound.
RewriteRule ^ (. *) $/bbs/$1

# Change yourdomain.com to the domain name you want to bind
# Change subfolder to the directory to be bound.
# Subfolder/is followed by index. php, index.html ......

RewriteCond % {HTTP_HOST} ^ (www .)? Mq35.com $
RewriteRule ^ (/)? $ Bbs/index. php [L]

The complete statement above is bound to a sub-directory. To bind multiple sub-directories, you can add the full set of statements

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.