什么是mod_rewrite?

mod_rewrite是apache一个允许服务器端对请求url做修改的模块。入端url将和一系列的rule来比对。这些rule包含一个正则表达式以便检测每个特别的模式。如果在url中检测到该模式,并且适当的预设条件满足,name该模式将被一个预设的字符串或者行为所替换。

这个过程持续进行直到没有任何未处理的规则或者该过程被显式地停止。

这可以用三点来总结:

  • 有一系列的顺序处理的规则rule集
  • 如果有一条规则被匹配,将同时检查该规则对应的条件是否满足
  • 如果一切处理结果都是go,那么将执行一条替换或者其他动作

mod_rewrite的好处

有一些比较明显的好处,但是也有一些并不是很明显:

mod_rewrite非常普遍地被用于转换丑陋的,难以明义的URL,形成所谓"友好或干净的url"。

另一方面,这些转换后的url将会是搜索引擎友好的

正则表达式token:

\s{2,}  2个以上的空格

\|    backward referrence

\\    matches a '\'

\b    word boundary position,比如whitespace或者字符串的开始或者结束

\B    Not a word boundary position

(?=ABC)  positive lookahead. Matches a group after your main expression without including it in the result

(?!ABC)  Negative lookahead.Specifies a group that can not match after your main expression(ie. if it matches, the result is discarded)

(?<=ABC)   Positive lookbehind. Matches a group before your main expression without including it in the result.

(?<!ABC)  Negative lookbehind.Specifies a group that can not match before your main expression(ie.if it matches, the result is discarded)

*?    :match zero or more of the preceeding token. This is a lazy match, and will match as few characters as possible before satisfying the next token

+?    :match one or more of the preceeding token. This is a lazy match, and will match as few characters as possible before satisfying the next token

{5}    :matches exactly 5 of the preceeding token;

{2,5}  : matches 2 to 5 of the preceding token. Greedy match;

{2,5}?   matches 2 to 5 of the preceding token. lazy match;

(ABC)  groups multiple tokens together. This allows you to apply quantifiers to the fall group. Creates a capture group roll over a match highlight to see the capture group result

(?:ABC)  groups multiple tokens without creating capture group;

$$    escaped $ symbol         $`: insert the portion of the string that precedes the match

$&:     inserts the matched substring           $' : insert the portion of the string that follows the match
[$1]:     inserts the result of the first capture group

m      multiline

i       ignore case

"S"      match any character, except for line breaks if dotall is false

"g"    search globally

var str='The price of tomato is 5, the price of apple is 10';
str.replace(/(\d+)/g, '$1.00'); // 5.00 10.00

?     zero or one

\    escape

\.  \\   \+  \*  \?  \^  \$  \[  \]  \(  \)  \{  \}  \/  \'  \#

[ABC]  Any single character in ABC set

/th(a|i)nk/=/th[ai]nk/

() :捕获 /(.+)@(163|126|188)\.com$/ 检查网易邮箱的格式

(?:)不捕获 /(.+)@(?:163|126|188)\.com$/

javascript中的str.match(regexp)获取被捕获的字符串以便使用

var url='http://blog.163.com/album?id=1#comment';
var reg=/(https?:)\/\/([^\/]+)(\/[^\?]*)?(\?[^#]*)?(#.*)?/;
var arr=rul.match(reg);
var protocal = arr[1]; //http
var host=arr[2];//blog.163.com
var pathname=arr[3]; // /album
var search=arr[4]; // id=1
var hash=arr[5]; //#comment

+    one or more

*    zero or more

|  or    matches the full before or after '|'      (https?|ftp)://

^    matches the beginning of the string    

$    matches the end of the string

$1    refer to a match

$2    refer to another match

?:  within parenthesis to not capture (^.+(?:jpg|png|gif)$)

[^ABC]   Any single character not in the set

[a-z]     any single character in the a-z range

[^b-e]    any single character that is not in range b-e

[0-9]

[\w'-]  any world characater, single quote or -

\t  \r\n  tab

\xFF    specifying a character by its hexdecimal index

\xA9 => copyright symbol

如何匹配不包含连续出现的一串字符串?

^(?!.*ab).*$  :不匹配ab连续出现

如何lazy模式尽可能少的匹配到字符串?

alert( "123 456".match(/\d+ \d+?/g) ); // 123 4

注意上面代码中的?就起到了数字匹配lazy最少的模式!

http://javascript.info/regexp-greedy-and-lazy

https://24ways.org/2013/url-rewriting-for-the-fearful/ 号称是最适合人来阅读的关于url-rewrite的文章

apache url rewrite及正则表达式笔记的更多相关文章

  1. apache url rewrite 的RewriteRule参数详解

    apache url rewrite 的RewriteRule参数详解 我们经常会在apache那边对一些url进行一些重写操作,那么就会涉及到很多的重写策略的选择,比如重定向的方式,参数的处理,规则 ...

  2. apache url rewrite问题

    apache RewriteEngine Your browser sent a request that this server could not understand http://www.ra ...

  3. Apache URL rewrite 配置

    下面是Apache的配置过程,可以参考下:1.httpd.conf配置文件中加载了mod_rewrite.so模块,使用虚拟主机 #LoadModule rewrite_module modules/ ...

  4. nginx和apache下的url rewrite

    将服务器上面的数据同步到本地之后,发现打开首页显示不正常,本地服务器是apache,经过打开url rewrite之后本地首页正常显示. 原因是phpwind本身支持了url rewrite的功能,但 ...

  5. apache开启url rewrite模块

    在把服务器数据转移到本地服务器之后,本地打开首页出现排版紊乱等问题,经过大神指点说是url rewrite的问题. 本篇文章主要写怎样开启apache的url rewrite功能. 打开Apache2 ...

  6. IIS 7.5 使用URL Rewrite模块简单设置网页跳转

    原文 IIS 7.5 使用URL Rewrite模块简单设置网页跳转 我们都知道Apache可以在配置文件里方便的设置针对网页或网站的rewrite,但是最近接手了一组IIS服务器,发现这货简单的没有 ...

  7. Apache之Rewrite和RewriteRule规则梳理以及http强转https的配置总结

    一. 简单实例介绍一般来说,apache配置好http和https后,如果想要做http强转到https,需要设置url重定向规则,大致需要下面几个步骤即可完成配置: 1)在httpd.conf文件里 ...

  8. Linux--nginx域名绑定-url rewrite

    进入/usr/local/nginx/conf 编辑 nginx.conf 绑定域名: 添加一个 server元素,更改后的配置内容可能如下: server { listen       80; se ...

  9. (Nginx) URL REWRITE

    URL重写的基础介绍 把URI地址用作参数传递:URL REWRITE 最简单的是基于各种WEB服务器中的URL重写转向(Rewrite)模块的URL转换: 这样几乎可以不修改程序的实现将 news. ...

随机推荐

  1. (01)JVM-内存三大核心区域以及分析

    package org.burning.sport.jvm; /** *  从JVM调用的角度分析Java程序对内存空间的使用, * 当JVM进程启动的时候,会从类加载器路径中找到包含main方法的入 ...

  2. [转]SQL的主键和外键约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  3. Nginx配置访问权限

    基于IP配置Nginx的访问权限 Nginx配置通过两种途径支持基本访问权限的控制,其中一种是由HTTP标准模块ngx_http_access_module支持的,通过IP来判断客户端是否拥有对Ngi ...

  4. 转--log4j.properties 详解与配置步骤

    一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...

  5. [心平气和读经典]The TCP/IP Guide(005)

    The TCP/IP Guide[Page 47, 48, 49] I created The TCP/IP Guide to provide you with an unparalleled bre ...

  6. 面试:TCP和UDP协议

    目录 TCP 协议 UDP协议 TCP和UDP的区别 TCP和UDP的使用场景 一 TCP协议 1.TCP的头部格式 理解TCP协议,首要的就是TCP协议的头部格式 ·        Source P ...

  7. 玩转mongodb(二):mongodb基础知识

    常用基本数据类型: null null用于表示空值或者不存在的字段: {"data":null} 布尔型 布尔类型只有两个值,true和false: {"data&quo ...

  8. 转载《浅析MVC框架中View层的优雅设计及实例》

    在基于B/S的应用程序开发中,从基本的技术分工上来说就是两大块,一是软件显示界面,另一个是程序逻辑.在N年前的脚本语言时代,无论是asp.php还是jsp,我们基本是都是把这两者柔和在一起的.尽管我们 ...

  9. 在C#中使用依赖注入

    依赖注入(Dependency Injection,缩写为DI)是一种实现(Inversion of Control,缩写为IoC)的方法.在编写C#代码时,使用这种方法能够解决一些场景的需求.本系列 ...

  10. 阿里巴巴Java开发手册(详尽版)-个人未注意到的知识点(转)

    转自 https://blog.csdn.net/u013039395/article/details/86528164 一.编程规约 (一) 命名风格 [强制]代码中的命名只可用英文方式 [强制]类 ...