Apache Nginx URL Address rewrite

Source: Internet
Author: User

URL rewriting this thing has been used many times in the work, but every time forget to remember to accumulate the knowledge.

Hey, either think not necessary, or just don't have the time?!

One, Apache article

Official address: http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_rewrite.html

1.htaccess Basic Syntax Introduction

The server has a configuration file can not be changed by us, so in most cases, in the root directory of the site to build a. htaccess file.

#设置重写的根目录
Rewritebase/
#开启重写引擎
Rewriteengine on

#RewriteCond Match all eligible requests

#如果不是文件, not directory
Rewritecond%{request_filename}!-f
Rewritecond%{request_filename}!-d
#执行RewriteRule规则体
#如果命中了这条, all of the following rewrite rules are not executed
Rewriterule ^/?front//front/index.html [L]
#下面两条命中 will be executed.
Rewriterule ^/?front/abc/background/abc.html
Rewriterule ^/?front/edf/background/edf.html


#如果不是文件, not directory
Rewritecond%{request_filename}!-f [NC]
Rewritecond%{request_filename}!-d [NC]
#执行RewriteRule规则体
Rewriterule. index.php [L]

Where Rewritecond is the if logic in the programming language, Rewriterule represents the code body. [L] stands for break. [NC] label indicates case insensitive

Similar logic:

2, Apache mod_rewrite rules rewrite the logo at a glance
1) R[=code] (force redirect) forced external redirection
Forces a http://thishost[:thisport]/prefix to be redirected to an external URL in an alternate string. If code is not specified, the default 302 HTTP status code will be used.
2) F (force URL to is forbidden) disables the URL and returns the 403HTTP status code.
3) G (force URL to is gone) forces the URL to gone and returns the 410HTTP status code.
4) P (force proxy) enforces the use of proxy forwarding.
5) L (last rule) indicates that the current rule is the final rule, stopping the rewrite of the rule after parsing.
6) N (next round) re-run the rewrite process starting with the first rule.
7) 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.
8) T=mime-type (Force MIME type) enforce MIME type
9) NS (used only if no internal sub-request) is only used for internal sub-requests
NC (no case) is not casing-sensitive
One) 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 be able to correctly convert/foo/zoo to/bar?arg=p1=zoo
PT (pass through to next handler) is passed 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) set environment variables


3. Apache built-in variable description

Http://blog.sina.com.cn/s/blog_6bad64c20101a3n9.html

4. Apache Use Example

Http://www.jb51.net/article/47907.htm

Second, Nginx article

Official address: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

Add the following to the Nginx [/data/servers/nginx/conf/domains/www.demo.com.conf] configuration to enable the. htaccess_nginx file to be supported.

Include/home/wwwroot/www.demo.com/.htaccess_nginx;
1.htaccess_nginx Basic Syntax Introduction

. htaccess_nginx File Contents

if($request _filename ~"^.*. (forum\.php). *$") {rewrite^/forum.php/(. *) $/forum.php?s=/$1Last ;  Break;}#and the conditional set $flag0;if(!-e $request _filename) {Set $flag"${flag}1";}if($request _filename ~"^.*. (/front/). *$") {Set $flag"${flag}2";}if($flag ="012") {rewrite^/front/(. *) $/front/index.html last;  Break;}if(!-e $request _filename) {Rewrite^/(.*)$ /index.php last;}

See more: http://www.nginx.cn/216.html

2,Nginx rewrite rules rewrite logo at a glance

1) Last stop processing the subsequent rewrite instruction set, and then re-locate the new URI for the current override on the rewrite instruction set.
2) Break stops processing subsequent rewrite instruction sets and is not re-searched, but the remaining non-rewrite statements in the current location and non-rewrite statements outside of the address can be executed.
3) Redirect if replacement does not start with HTTP.//or https://, returns 302 temporary redirect
4) permant return 301 Permanent redirect

3, Nginx built-in variable description

Http://www.cnphp.info/nginx-embedded-variables-lasted-version.html

4.Nginx Use Example

http://blog.csdn.net/wave_1102/article/details/46483897

Iii. Comparison of Apache and Nginx

The URL rewrite logic for the same effect that the two servers implement is a comparison chart:

Same point:

Use common regular syntax for URL matching.

Different points:

NGINGX cannot be used and the statement. You need to use a SET statement to handle it.

The corresponding tags and syntax keywords are different.

Ps:

Http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_rewrite.html

Http://www.runoob.com/regexp/regexp-syntax.html

Http://www.cnblogs.com/yuanzai12345/p/5976090.html

Apache Nginx URL Address rewrite

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.