伪静态正则写法

伪静态在线生成:    http://www.bm8.com.cn/webtool/htaccess/      

  • 在正是写规则前,先为大家讲一下重写中设计到的特殊字符的含义。和普通正则是通用的!
  • *代表前面0或更多个字符
  • +代表前面1或更多个字符
  • ?代表前面0或1个字符
  • ^代表字符串的开始位置
  • $代表字符串结束的位置
  • .为通配符,代表任何字符
  • \将跟在其后的字符还原为字符本身,例如“\+”代表的就是“+”,而非其它意思。
  • ^在方括号里表示非的意思。例如[^.]代表非通配符。

平时常用的:

~      为区分大小写的匹配。
~*     不区分大小写的匹配(匹配firefox的正则同时匹配FireFox)。
!~     不匹配的
!~*    不匹配的

.     匹配除换行符以外的任意字符
\w     匹配字母或数字或下划线或汉字
\s     匹配任意的空白符
\d     匹配数字
\b     匹配单词的开始或结束
^     匹配字符串的开始
$     匹配字符串的结束

*     重复零次或更多次
+     重复一次或更多次
?     重复零次或一次
{n}     重复n次
{n,}     重复n次或更多次
{n,m}     重复n到m次
*?     重复任意次,但尽可能少重复
+?     重复1次或更多次,但尽可能少重复
??     重复0次或1次,但尽可能少重复
{n,m}?     重复n到m次,但尽可能少重复
{n,}?     重复n次以上,但尽可能少重复

\W     匹配任意不是字母,数字,下划线,汉字的字符
\S     匹配任意不是空白符的字符
\D     匹配任意非数字的字符
\B     匹配不是单词开头或结束的位置
[^x]     匹配除了x以外的任意字符

这次多添加了两条新规则

1:访问wojiuwangla.com/wangla.html跳转到百度

2:访问wojiuwangla.com/纯数字至少一个数字.html跳转到QQ官网

3:访问wojiuwangla.com/匹配字母或数字或下划线组合.html 跳转到问道官网

学会了这两条规则配置,nginx的伪静态规则配置基本就大专毕业了,哈哈哈哈…
场景一:
http://www.abc.com/index.php/front/index/index
重写成 http://www.abc.com/a.html

场景二:把带参数的1.2解析成3
1.http://www.abc.com/index.php/front/index/parse/name/yangxignyi/age/18
2.http://www.abc.com/index.php/front/index/parse?name=yangxignyi&age=18
3.http://www.abc.com/parse-yangxignyi-18.html
服务器配置文件:

server{
listen 80;
server_name www.abc.com;
root "D:/phpStudy/WWW/abc";
location / {
index index.php index.htm /public/index.html;
autoindex off;
include abc.conf;
#rewrite a.html /index.php/front/index/index last;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}

伪静态配置文件可以直接写在 location / {} 里面的,不推荐这样做,建议新增加个rewrite.conf写伪静态文件会好点,include 进来就行了,这样可以在rewrite.conf里面写n多配置

location / {
index index.php index.htm /public/index.html;
autoindex off;
include rewrite.conf;
#rewrite a.html /index.php/front/index/index last;

#rewrite.conf (这个文件自己创建就行了,文件内容写规则)

#场景一的规则
#http://www.abc.com/index.php/front/index/index
rewrite a.html /index.php/front/index/index last;

#场景二的规则
#1.http://www.abc.com/index.php/front/index/parse/name/yangxignyi/age/18
#2.http://www.abc.com/index.php/front/index/parse?name=yangxignyi&age=18
#3.http://www.abc.com/parse-yangxingyi-18.html
rewrite parse-(\w+)-(\d+).html /index.php/front/index/parse/name/$1/age/$2 last;

相信看过正则的同学都知道\w+和\d+是什么意思的吧,\w是数字字母下划线的意思,\d是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数,关于正则这里就不多介绍啦,看下正则就知道了哦 !!!

写完规则,记得重启nginx,才能去吃饭哦!!!

猜你喜欢

转载自www.cnblogs.com/lingen163/p/10979125.html
今日推荐