Nginx/Apache之伪静态设置 - 运维小结

一、什么是伪静态

伪静态即是网站本身是动态网页如.php、.asp、.aspx等格式动态网页有时这类动态网页还跟"?"加参数来读取数据库内不同资料,伪静态就是做url重写操作(即rewrite)。很典型的案例即是discuz论坛系统,后台就有一个设置伪静态功能,开启伪静态后,动态网页即被转换重写成静态网页类型页面,通过浏览器访问地址和真的静态页面没区别。但是记住:做伪静态的前提就是服务器要支持伪静态重写URL Rewrite功能。

考虑搜索引擎优化(即SEO),将动态网页通过服务器处理成静态页面,如www.kevin.com/jk/fd.php?=12这样的动态网页处理成www.kevin.com/jk-fd-12.html这样格式静态页面,常见的论坛帖子页面,都是经过伪静态处理成静态页面格式html页面。由于网站所用的程序语言不易被发现,经过重写来伪静态来将动态网页的程序后缀变为html的静态页面格式。伪静态是一种可以把文件后缀改成任何可能的一种方法,比如如果想把php文件伪静态成html文件,这种配置相当简单的,后面会提到相应配置。

二、真静态、伪静态优点和缺点

真静态(html)优点;1)减少服务器对数据响应的负荷;2)加载不用调动数据库,响应速度快。

真静态缺点:1)维护不方便,每次都要手动生成;2)空间占用比较大,容易造成磁盘压力;3)生成的文件多,服务器对html文件的响应负担也较重。

伪静态(url重写)优点:1)可以方便的实现对化化引擎的优化,并且比生成静态更加方便;2)占空间比较小;3)首页每天都自动变化,不用维护。网站首页一般都有热点排行之类的,你可以设为,24小时排行,一周排行,再加上最新文章,最新点评等。这样首页天天是有变化的;4)便于广告的轮显。比如:你可以把 art1234.aspx,这个虚成n个页,如art_1234.aspx,news_1234.aspx,top_1234.aspx,在不同的页面放 不同的广告。总之是动态的,就可以随意动。

伪静态缺点:1)如果流量稍大一些使用伪静态就出现CPU使用超负荷,因为伪静态是用正则判断而不是真实地址,分辨到底显示哪个页面的责任也由直接指定转由CPU来判断了,所以CPU占有量的上升,确实是伪静态最大的弊病;2)伪静态效率不如生成html的,因为它不是真正意义上的静态页,所以每次请求都是要去读取数据库的信息(这个可以用缓存技术来补偿一下)。

三、真静态、伪静态原理与实现方案

1、伪静态

伪静态是相对于真静态而言的,就是把一些asp,php等结尾url通过apche或nginx的重写规则,变成以html一类的静态页面形式。伪静态不是真正的静态,它和动态地址一样要读取数据库。伪静态最主要的作用就是利于seo,百度spider(百度蜘蛛)喜欢抓取静态页面,可容易使百度spider陷入死循环;并发量高的时候会加大服务器的压力,所以用的时候要注意。

伪静态就是利用apche,nginx重写规则,对url地址重写实现的!伪静态实现原理:

1) Apache伪静态前提是要打开apache的重写模块 (即打开"LoadModule rewrite_module modules/mod_rewrite.so"这一行);

2) Nginx默认就支持伪静态;

伪静态有两种配置方式

1) 在配置虚拟主机的时候设置;

2) 在web根目录下创建一个.htaccess文件,在这个文件里面配置;

2、真静态

在网站设计中,纯粹HTML(标准通用标记语言下的一个应用)格式的网页通常被称为"静态网页",静态网页是标准的HTML文件,它的文件扩展名是.htm、.html,可以包含文本、图像、声音、FLASH动画、客户端脚本和ActiveX控件及JAVA小程序等。静态网页是网站建设的基础,早期的网站一般都是由静态网页制作的。静态网页是相对于动态网页而言,是指没有后台数据库、不含程序和不可交互的网页。静态网页相对更新起来比较麻烦,适用于一般更新较少的展示型网站。容易误解的是静态页面都是htm这类页面,实际上静态也不是完全静态,它也可以出现各种动态的效果,如GIF格式的动画、FLASH、滚动字幕等。

大型web项目优化中经常会考虑到使用真静态,这样在访问量大的时候,可以减少cpu的压力,但是会生成大量的文件占用网站的磁盘空间,可以写个php的脚本或用linux的计划任务进行删除。在用真静态的时候有的时候需要用到局部的动态化。

真静态实现方法

1)利用PHP模板生成静态页面;

2)使用PHP文件读写功能生成静态页面;

3)使用PHP输出控制函数缓存机制生成静态页面;

4)使用nosql从内存中读取内容(其实这个已经不算静态化了而是缓存);

memcached是键值一一对应,key默认最大不能超过128个字节,value默认大小是1M,因此1M大小满足大多数网页大小的存储。

真静态和伪静态的区别

1)是不是一个真正静态页面;

2)有没有和数据库或后台程序进行交互;

3)它们的应用场景和解决的问题不同;

4)用javascript:alert(document.lastModified)来判断是真静态还是伪静态;

真静态在apache和nginx上的区别与否

1)真静态在nginx上的运行速度比apache运行速度快;

2)nginx处理静态文件对于apache来说消耗的内存少;

伪静态在apache和nginx上的区别与否

1)本质上没有区别,两者都是根据正则匹配对应的url的重写。但是apache和nginx上的伪静态规则还是有点不同,在配置的时候要注意;

2)apache处理伪静态比nginx更有优势;

先来看个简单的小例子

?

1

2

3

4

5

6

7

8

跳转需求:

访问http://www.kevin.com/p/123456.html 跳转成 http://a.aa.com/p/123456

配置如下:

rewrite ^/p/(\d+).html http://www.kevin.com/p/$1 last;

解释说明:

\d是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。

四、Nginx伪静态配置和常用Rewrite伪静态规则演示集锦

nginx防盗链

?

1

2

3

4

5

6

7

location ~* \.(gif|jpg|png)$ {

valid_referers none blocked http://kevin.com:81;#允许访问的来源,即所有来自http://kevin.com:81的gif|jpg|png结尾的文件

if($invalid_referer) {

rewrite ^/ http://kevin.com:81/c2_.html;

#return 403;

}

}

伪静态重写

?

1

2

3

4

5

location / {

rewrite c(\d+)_(.*).html/index.php?c=user&m=index&id=$1&title=$2 last;

root/usr/share/nginx/html/sta;

index index.html index.htm index.php;

}

对于生成大量的大量html文件,推荐使用rsync工具进行批量删除,做法如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

1) 建立一个空目录

# mkdir -p /root/kevin/tmp/

2) 确认需要清空的目标目录(即需要删除的大量html文件所在的目录),比如/root/kevin/tmp1/

3)使用rsync同步删除(注意目录后面的“/”),整体效率会快一个数量级的样子。

# rsync --delete-before -a -H /root/kevin/tmp/ /root/kevin/tmp1/

选项说明:

–delete-before 接收者在传输之前进行删除操作

–progress 在传输时显示传输过程

-a 归档模式,表示以递归方式传输文件,并保持所有文件属性

-H 保持硬连接的文件

-v详细输出模式

-stats 给出某些文件的传输状态

如下操作可以把某个超大文件删掉,比如删除/mnt/ops/web.html文件

1)建立空文件/mnt/ops/html.txt

2)# rsync --delete-before -a -H -v --progress --stats /mnt/ops/html.txt /mnt/ops/web.html

3)这样置为空后就可以快速删掉

原理:

把文件系统的目录与书籍的目录做类比,rm删除内容时,将目录的每一个条目逐个删除(unlink),需要循环重复操作很多次;

rsync删除内容时,建立好新的空目录,替换掉老目录,基本没开销。

测试大文件删除

1)生成文件

# for i in $(seq 1 500000); do echo test >>/opt/test/$i.txt; done

测试结果:

10万文件rm删除第一次11s左右,第二次9s左右;rsync测试两次9s左右

50万文件rm删除报错;rsync测试两次一个5分左右,一个4分左右

rm删除命令

# time rm -f /opt/test/*.txt

rsync命令删除

# mkdir /opt/test1

# rsync --delete-before -a -H -v --progress --stats /opt/test1/ /opt/test/

nginx php thinkphp5 伪静态配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

server {

listen 80;

server_name 127.0.0.1 localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

if(!-e $request_filename) {

#一级目录

#rewrite ^/(.*)$ /index.php?s=$1 last;

#二级目录

rewrite ^/TWcloud/public/(.*)$/TWcloud/public/index.php?s=$1 last;

break;

}

root/Applications/MxSrvs/www;

index index.html index.htm index.php;

}

location ~ \.php {#去掉$

root/Applications/MxSrvs/www;

fastcgi_pass 127.0.0.1:10080;

fastcgi_index index.php;

fastcgi_split_path_info ^(.+\.php)(.*)$;#增加这一句

fastcgi_param PATH_INFO $fastcgi_path_info;#增加这一句

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

thinkphp 5.0的nginx伪静态规则 (踩过坑点,经测试实现)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

server {

listen 80;

server_name www.kevin.com;

root/data/www/web;

location / {

index index.html index.htm index.php default.php;

#重点就是加入下面这个if

if(!-e $request_filename){

rewrite ^(.*)$/index.php?s=$1 last;break;

}

}

location ~ .php(.*)$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.html;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_split_path_info ^(.+.php)(.*)$;

fastcgi_param PATH_INFO $fastcgi_path_info;

include fastcgi_params;

}

}

nginx伪静态配置小案例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

场景一:

http://www.abc.com/index.php/front/index/index

重写成

http://www.abc.com/a.html

场景二:

把下面带参数的第1、2个url解析成第3个url

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/data/www/web;

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/indexlast;

#场景二的规则

#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是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。

nginx配置伪静态的Rewrite重写的正则使用说明

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

正则表达式匹配 :

~ 为区分大小写的匹配

~* 不区分大小写的匹配(匹配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以外的任意字符

文件及目录匹配,其中:

-f和!-f 用来判断是否存在文件

-d和!-d 用来判断是否存在目录

-e和!-e 用来判断是否存在文件或目录

-x和!-x 用来判断文件是否可执行

flag标记有:

last 相当于Apache里的[L]标记,表示完成rewrite

break终止匹配, 不再匹配后面的规则

redirect 返回302临时重定向 地址栏会显示跳转后的地址

permanent 返回301永久重定向 地址栏会显示跳转后的地址

$args 此变量与请求行中的参数相等

$content_length 等于请求行的“Content_Length”的值。

$content_type 等同与请求头部的”Content_Type”的值

$document_root 等同于当前请求的root指令指定的值

$document_uri 与 $uri 一样

$host 与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样

$limit_rate 允许限制的连接速率

$request_method 等同于request的method,通常是“GET”或“POST”

$remote_addr 客户端ip

$remote_port 客户端port

$remote_user 等同于用户名,由ngx_http_auth_basic_module认证

$request_filename 当前请求的文件的路径名,由root或alias和URI request组合而成

$request_body_file

$request_uri 含有参数的完整的初始URI

$query_string 与 $args一样

$server_protocol 等同于request的协议,使用“HTTP/1.0”或“HTTP/1.1”

$server_addr request 到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。

$server_name 请求到达的服务器名

$server_port 请求到达的服务器的端口号

$uri 等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index

nginx伪静态配置案例集锦

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

nginx里使用伪静态是直接在nginx.conf 中写规则的,并不需要像apache要开启写模块(mod_rewrite)才能进行伪静态。

nginx只需要打开nginx.conf配置文件,在server里面写需要的规则即可。

1)配置案例1

server {

listen 80;

server_name www.kevin.com;

index index.html index.htm index.php;

rewrite ^/wangla.html$ http://www.baidu.com/ permanent;

rewrite ^/(\d+).html$ http://www.qq.com/ permanent;

rewrite ^/(\w+).html$ http://wd.gyyx.cn/index_wd_v5.htm permanent;

}

以上添加了几条重写规则

访问www.kevin.com/wangla.html跳转到百度

访问www.kevin.com/纯数字至少一个数字.html跳转到QQ官网

访问www.kevin.com/匹配字母或数字或下划线组合.html 跳转到问道官网。

2)配置案例2

server {

listen 80;

server_name bbs.jb51.net;

index index.html index.htm index.php;

root/home/www/bbs;

error_page 404/404.htm;#配置404错误页面

location ~ .*.(php|php5)?$ {

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

#下面就是伪静态了

location /{

rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;

}

access_log access_log off;

}

然后重启nginx服务器伪静态就生效了,这种维护起来很是不方便我们可以把它写在外部文件如bbs_nginx.conf中

/home/www/bbs目录下创建bbs_nginx.conf文件并写入以下代码:

location /{

rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;

}

然后在上面的代码后面加上如下代码:

include/home/www/bbs/bbs_nginx.conf;

这样网站根目录中的bbs_nginx.conf伪静态规则,即可实现单独管理。

下面是一个实例:

在使用.htaccess文件的目录下新建一个.htaccess文件,如下面一个Discuz论坛目录:

# vim /var/www/html/jb51/bbs/.htaccess

rewrite ^(.*)/archiver/((fid|tid)-[w-]+.html)$ $1/archiver/index.php?$2 last;

rewrite ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 last;

rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;

rewrite ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 last;

rewrite ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 last;

rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last;

接着修改nginx配置文件:

# vim /etc/nginx/nginx.conf

在需要添加伪静态的虚拟主机的server{}中引入.htaccess文件:

include/var/www/html/jb51/bbs/.htaccess;

最后重新加载nginx配置文件:

# /etc/init.d/nginx reload

3)下面是一些rewrite配置集锦,供运维参考:

=======================================================================================

rewrite"^/(.{6})(\d{3})(.+)/php/"http://www.kevin.com/qq$2.apkbreak;

中间用到了{6}指前面的字符得复6次

结合PHP的例子

if(!-d $request_filename) {

rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$/index.php?namespace=user&controller=$1&action=$2&$3 last;

rewrite ^/([a-z-A-Z]+)/?$/index.php?namespace=user&controller=$1 last;

break;

多目录转成参数。

abc.domian.com/sort/2=> abc.domian.com/index.php?act=sort&name=abc&id=2

配置如下:

if($host ~* (.*)\.domain\.com) {

set$sub_name $1;

rewrite ^/sort\/(\d+)\/?$/index.php?act=sort&cbash functions">id=$1 last;

}

目录对换

/123456/xxxx->/xxxx?id=123456

配置如下:

rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;

例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:

if($http_user_agent ~ MSIE) {

rewrite ^(.*)$/nginx-ie/$1break;

}

目录自动加"/"

if(-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

禁止htaccess

location ~/\.ht {

deny all;

}

禁止多个目录

location ~ ^/(cron|templates)/ {

deny all;

break;

}

禁止以/data开头的文件

可以禁止/data/下多级目录下.log.txt等请求;

location ~ ^/data{

deny all;

}

禁止单个目录

不能禁止.log.txt能请求

location/searchword/cron/{

deny all;

}

禁止单个文件

location ~/data/sql/data.sql {

deny all;

}

给favicon.ico和robots.txt设置过期时间;

这里为favicon.ico为99 天,robots.txt为7天并不记录404错误日志

location ~(favicon.ico) {

log_not_found off;

expires 99d;

break;

}

location ~(robots.txt) {

log_not_found off;

expires 7d;

break;

}

设定某个文件的过期时间;这里为600秒,并不记录访问日志

location ^~/html/scripts/loadhead_1.js {

access_log off;

root/opt/lampp/htdocs/web;

expires 600;

break;

}

文件反盗链并设置过期时间

这里的return412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求

"rewrite ^/ http://leech.c1gstudio.com/leech.gif;"显示一张防盗链图片

"access_log off;"不记录访问日志,减轻压力

"expires 3d"所有文件3天的浏览器缓存

location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {

valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;

if($invalid_referer) {

rewrite ^/ http://leech.c1gstudio.com/leech.gif;

return412;

break;

}

access_log off;

root/opt/lampp/htdocs/web;

expires 3d;

break;

}

只充许固定ip访问网站,并加上密码

root/opt/htdocs/www;

allow 218.197.16.14;

allow 22.133.11.22;

allow 21.112.69.14;

deny all;

auth_basic"C1G_ADMIN";

auth_basic_user_file htpasswd;

将多级目录下的文件转成一个文件,增强seo效果

/job-123-456-789.html 指向/job/123/456/789.html

配置如下:

rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$/job/$1/$2/jobshow_$3.html last;

--------------------------------------------------------------------------------

将根目录下某个文件夹指向2级目录

/shanghaijob/指向/area/shanghai/

如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/

配置如下:

rewrite ^/([0-9a-z]+)job/(.*)$/area/$1/$2 last;

上面例子有个问题是访问/shanghai时将不会匹配

rewrite ^/([0-9a-z]+)job$/area/$1/ last;

rewrite ^/([0-9a-z]+)job/(.*)$/area/$1/$2 last;

这样/shanghai也可以访问了,但页面中的相对链接无法使用,

如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。

那加上自动跳转也是不行的!

(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果。

if(-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

知道原因后就好办了,手动跳转吧

rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;

rewrite ^/([0-9a-z]+)job/(.*)$/area/$1/$2 last;

--------------------------------------------------------------------------------

文件和目录不存在的时候重定向:

if(!-e $request_filename) {

proxy_pass http://127.0.0.1/;

}

域名跳转

server {

listen 80;

server_name jump.c1gstudio.com;

index index.html index.htm index.php;

root/opt/lampp/htdocs/www;

rewrite ^/ http://www.c1gstudio.com/;

access_log off;

}

多域名转向

server_name http://www.c1gstudio.com/ http://www.c1gstudio.net/;

index index.html index.htm index.php;

root/opt/lampp/htdocs;

if($host ~"c1gstudio\.net") {

rewrite ^(.*) http://www.c1gstudio.com$1/ permanent;

}

三级域名跳转

if($http_host ~*"^(.*)\.i\.c1gstudio\.com$") {

rewrite ^(.*) http://top.yingjiesheng.com$1/;

break;

}

域名镜向

server

{

listen 80;

server_name mirror.c1gstudio.com;

index index.html index.htm index.php;

root/opt/lampp/htdocs/www;

rewrite ^/(.*) http://www.c1gstudio.com/$1 last;

access_log off;

}

某个子目录作镜向

location ^~/zhaopinhui{

rewrite ^.+ http://zph.c1gstudio.com/ last;

break;

}

discuz ucenter home (uchome) rewrite伪静态配置

rewrite ^/(space|network)-(.+)\.html$ /$1.php?rewrite=$2 last;

rewrite ^/(space|network)\.html$ /$1.php last;

rewrite ^/([0-9]+)$/space.php?uid=$1 last;

discuz 7 rewrite伪静态配置

rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;

rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;

rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\=$4&page=$3 last;

rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;

rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;

rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;

给discuz某版块单独配置域名

server_name bbs.c1gstudio.com news.c1gstudio.com;

location = / {

if($http_host ~ news\.c1gstudio.com$) {

rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;

break;

}

}

discuz ucenter 头像 rewrite 优化

location ^~/ucenter{

location ~ .*\.php?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

location/ucenter/data/avatar{

log_not_found off;

access_log off;

location ~ /(.*)_big\.jpg$ {

error_page 404/ucenter/images/noavatar_big.gif;

}

location ~ /(.*)_middle\.jpg$ {

error_page 404/ucenter/images/noavatar_middle.gif;

}

location ~ /(.*)_small\.jpg$ {

error_page 404/ucenter/images/noavatar_small.gif;

}

expires 300;

break;

}

}

jspace rewrite伪静态配置

location ~ .*\.php?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

location ~* ^/index.php/

{

rewrite ^/index.php/(.*)/index.php?$1break;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

Nginx伪静态配置案例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

公司域名bo.kevin.com下有多个子项目目录结构大致是bo.kevin.com/own/xys|bo.kevin.com/2017/abc|bo.kevin.com/2018/def有二级也有三级目录,

应开发需求某项目访问地址是:bo.kevin.com/own/xys/index.php/admin/login需要把index.php隐藏为bo.kevin.com/own/xys/index/admin/login进行访问。

设定以下三种场景:

场景一

将 http://www.abc.com/index.php/front/index/index

重写成 http://www.abc.com/a.html

场景二

将 http://www.abc.com/index.php/front/index/parse?name=itboy&age=18

重写成 http://www.abc.com/parse-itboy-18.html

场景三(同一域名下,需要匹配随时新增的二三级目录,并隐藏index.php的.php后缀)

将 http://bo.kevin.com/own/xys/index.php/admin/login以及 http://bo.kevin.com/2018/gdhp/index.php/login

重写成 http://bo.kevin.com/own/xys/index/admin/login以及 http://bo.kevin.com/2018/gdhp

建议在nginx/conf目录下新建rewrite.conf配置文件中编写伪静态规则,写完后在域名.conf文件中插入rewrite.conf文件即可(用include rewrite.conf插入)。

nginx配置文件如下:

server

{

listen 80;

server_name bo.kevin.com;

index index.html index.php index.htm index.php default.html default.htm default.php;

root/var/www/apps/bo.kevin.com;

include rewrite.conf;

include none.conf;

error_page 502/502.html;

includeenable-php-pathinfo.conf;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

location ~ .*\.(js|css)?$

{

expires 12h;

}

location ~ /\.

{

deny all;

}

access_log/var/www/wwwlogs/bo.kevin.com.log;

error_log/var/www/wwwlogs/error.bo.kevin.com.log;

}

rewrite.conf文件内容如下:

location /{

#场景一

#http://www.abc.com/index.php/front/index/index 变成 http://www.abc.com/a.html

rewrite a.html/index.php/front/index/indexlast;

#场景二

#http://www.abc.com/index.php/front/index/parse?name=itboy&age=18 变成 http://www.abc.com/parse-itboy-18.html

rewrite parse-(\w+)-(\d+).html/index.php/front/index/parse/name/$1/age/$2 last;

#场景三

#http://bo.kevin.com/own/xys/index.php/admin/login 以及 http://bo.kevin.com/2018/gdhp/index.php/login

#变成 http://bo.kevin.com/own/xys/index/admin/login 以及 http://bo.kevin.com/2018/gdhp/index/login

rewrite ^/(\w+)/(\w+)/(.*)$ /$1/$2/index.php?s=$3 last;#针对own目录伪静态规则,$1对应(\w+)部分,$2对应第二个(\w+)部分,$3对应(.*)部分,$代表直至最后

rewrite ^/(\d+)/(\w+)/(.*)$ /$1/$2/index.php?s=$3 last;#针对后期的2018下的子项目伪静态规则

}

说明:

其实都是很简单的对号入座原理而已,拿场景二来说明,第一个正则(\w+)对应的就是$1,第二个正则(\d+)对应的就是$2,

另外,\w是数字字母下划线的意思,\d是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。

Nginx上支持.htaccess伪静态的配置实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# vim /usr/local/nginx/conf/vhost/web.conf

........

include/var/www/web/.htaccess

# vim /var/www/web/.htaccess

rewrite ^/show-([0-9]+)-([0-9]+)\.html$/index.php?action=show&id=$1&page=$2;

rewrite ^/category-([0-9]+)-([0-9]+)\.html$/index.php?action=index&cid=$1&page=$2;

rewrite ^/archives-([0-9]+)-([0-9]+)\.html$/index.php?action=index&setdate=$1&page=$2;

rewrite ^/(archives|search|reg|login|index|links)\.html$/index.php?action=$1;

rewrite ^/(comments|tagslist|trackbacks|index)-([0-9]+)\.html$/index.php?action=$1&page=$2;

if($host !='www.shibo.com') {

rewrite ^/(.*)$ http://www.shibo.com/$1 permanent;

}

error_page 404 http://www.shibo.com/;

# /usr/local/nginx/sbin/nginx -s reload

解决nginx配置伪静态(去除框架的Index.php)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

在nginx.conf中Location/{}中加上下面这个if判断就可以了:

location /{

if(!-e $request_filename) {

rewrite ^(.*)$/index.php?s=$1 last;break;

}

}

===============================================================================

如下面一个配置

if(!-e $request_filename) {

rewrite ^/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\.html/index.php?m=$1&c=$2&a=$3&$4=$5&$6=$7 last;

break;

}

Nginx 常用伪静态配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

访问 http://www.kevin.com/sort/15.html -> http://www.kevin.com/1.php?id=15

location / {

root/usr/share/nginx/html/1;

index index.html;

rewrite/sort/(.*)\.html/1.php?id=$1 last;

}

================================================================================

再如下面一个配置

server {

listen 80 default_server;

server_name _;

location / {

root/usr/share/nginx/html;

index index.html index.htm;

rewrite ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3;

}

}

策略:RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3

请求路径:http://www.abc.com/list-123-456.html

以上策略分成两段:

第一段是使用正则表达式去匹配请求访问的路径,第二段是将匹配后的参数转化为真实访问的路径。

策略执行时:^(.*)list-([0-9]+)-([0-9]+)\.html$ 与/list-123-456.html 这个字符串进行匹配:

^和$字符分别代表了匹配输入字符串的开始和结束;

()中的匹配到的内容会被按顺序分配到变量$1 $2 $3中;

.*匹配任意字符串,且长度从0个到多个,故$1值为/;

[0-9]+匹配字符0-9,长度1个到多个,故$2和$3分别是123和456;

所以最后真实访问的动态地址为/list.php?page=123&id=456

================================================================================

再如下面一个配置

1)/a/b?c=d => index.php?_a=a&_m=b&c=d

2)/xxx/detail-yyy.html => index.php?_a=xxx&_m=detail&id=yyy

配置如下:

server {

listen 80;

server_name my.xh5.com;

location / {

root/mnt/hgfs/web/my.xueh5.com/src/;

index index.html index.htm index.php;

if($args ~"^(.*)$"){

set$rule_0 1$rule_0;

set$bref_1 $1;

}

if($rule_0 ="1"){

rewrite ^([0-9a-zA-Z]*)/detail-([0-9]*)\.html$/index.php?_a=$1&_m=detail&id=$2&$bref_1 last;

rewrite ^/([0-9a-zA-Z]*)/([0-9a-zA-Z.]*)$/index.php?_a=$1&_m=$2&$bref_1 last;

rewrite ^/([0-9a-zA-Z]+)$/index.php?_a=$1&_m=index&$bref_1 last;

rewrite ^/$/index.php?_a=index&_m=index&$bref_1 last;

}

}

location ~ \.php$ {

root/mnt/hgfs/web/my.xueh5.com/src/;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

Nginx常用伪静态规则小总结

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

1)WordPress伪静态

if(-f $request_filename/index.html){

rewrite (.) $1/index.htmlbreak;

}

if(-f $request_filename/index.php){

rewrite (.) $1/index.php;

}

if(!-f $request_filename){

rewrite (.*)/index.php;

}

2)PHPCMS伪静态

rewrite ^/caipu-([0-9]+)-([0-9]+)-([0-9]+).html/index.php?m=content&c=index&a=show&catbash functions">id=$2&page=$3 last;

rewrite ^/content-([0-9]+)-([0-9]+)-([0-9]+).html/index.php?m=content&c=index&a=show&catbash functions">id=$2&page=$3 last;

rewrite ^/list-([0-9]+)-([0-9]+).html/index.php?m=content&c=index&a=lists&catid=$1&page=$2 last;

rewrite ^/tag-([^.])-([0-9]+)-([0-9]+).html/index.php?m=content&c=tag&catid=$2&tag=$1&page=$3 last;

rewrite ^/comment-([0-9]+)-([0-9]+)-([0-9]+).html/index.php?m=comment&c=index&a=init&commentid=content_$1-$2-$3 last;

rewrite ^/([^.]).html/index.php?m=member&c=index&a=$1 last;

3)DEDECMS伪静态

rewrite"^/index.html$"/index.php last;

rewrite"^/list-([0-9]+).html$"/plus/list.php?tid=$1 last;

rewrite"^/list-([0-9]+)-([0-9]+)-([0-9]+).html$"/plus/list.php?tid=$1&totalresult=$2&PageNo=$3 last;

rewrite"^/view-([0-9]+)-1.html$"/plus/view.php?arcID=$1 last;

rewrite"^/view-([0-9]+)-([0-9]+).html$"/plus/view.php?aid=$1&pageno=$2 last;

rewrite"^/tags.html$"/tags.php last;

rewrite"^/tag-([0-9]+)-([0-9]+).html$"/tags.php?/$1/$2/ last;

4)Discuz7伪静态

rewrite ^/archiver/((fid|tid)-[\w-]+.html)$/archiver/index.php?$1 last;

rewrite ^/forum-([0-9]+)-([0-9]+).html$/forumdisplay.php?fid=$1&page=$2 last;

rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$/viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;

rewrite ^/space-(username|uid)-(.+).html$/space.php?$1=$2 last;

rewrite ^/tag-(.+).html$/tag.php?name=$1 last;

5)DiscuzX伪静态

rewrite ^([^.])/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last;

rewrite ^([^.])/article-([0-9]+)-([0-9]+).html$ $1/portal.php?mod=view&aid=$2&page=$3 last;

rewrite ^([^.])/forum-(\w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

rewrite ^([^.])/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;

rewrite ^([^.])/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last;

rewrite ^([^.])/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last;

rewrite ^([^.]*)/([a-z]+)-(.+).html$ $1/$2.php?rewrite=$3 last;

if(!-e $request_filename) {

return404;

}

6)ECSHOP伪静态

if(!-e $request_filename)

{

rewrite"^/index.html"/index.php last;

rewrite"^/category$"/index.php last;

rewrite"^/feed-c([0-9]+).xml$"/feed.php?cat=$1 last;

rewrite"^/feed-b([0-9]+).xml$"/feed.php?brand=$1 last;

rewrite"^/feed.xml$"/feed.php last;

rewrite"^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-])-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 last;

rewrite"^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-])(.).html$"/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 last;

rewrite"^/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/category.php?id=$1&brand=$2&page=$3&sort=$4&order=$5 last;

rewrite"^/category-([0-9]+)-b([0-9]+)-([0-9]+)(.).html$"/category.php?id=$1&brand=$2&page=$3 last;

rewrite"^/category-([0-9]+)-b([0-9]+)(.).html$"/category.php?id=$1&brand=$2 last;

rewrite"^/category-([0-9]+)(.).html$"/category.php?id=$1 last;

rewrite"^/goods-([0-9]+)(.).html"/goods.php?id=$1 last;

rewrite"^/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/article_cat.php?id=$1&page=$2&sort=$3&order=$4 last;

rewrite"^/article_cat-([0-9]+)-([0-9]+)(.).html$"/article_cat.php?id=$1&page=$2 last;

rewrite"^/article_cat-([0-9]+)(.).html$"/article_cat.php?id=$1 last;

rewrite"^/article-([0-9]+)(.).html$"/article.php?id=$1 last;

rewrite"^/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+).html"/brand.php?id=$1&cat=$2&page=$3&sort=$4&order=$5 last;

rewrite"^/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.).html"/brand.php?id=$1&cat=$2&page=$3 last;

rewrite"^/brand-([0-9]+)-c([0-9]+)(.).html"/brand.php?id=$1&cat=$2 last;

rewrite"^/brand-([0-9]+)(.).html"/brand.php?id=$1 last;

rewrite"^/tag-(.).html"/search.php?keywords=$1 last;

rewrite"^/snatch-([0-9]+).html$"/snatch.php?id=$1 last;

rewrite"^/group_buy-([0-9]+).html$"/group_buy.php?act=view&id=$1 last;

rewrite"^/auction-([0-9]+).html$"/auction.php?act=view&id=$1 last;

rewrite"^/exchange-id([0-9]+)(.).html$"/exchange.php?id=$1&act=view last;

rewrite"^/exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/exchange.php?cat_bash functions">sort=$5&order=$6 last;

rewrite ^/exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/exchange.php?cat_bash functions">sort=$3&order=$4 last;

rewrite"^/exchange-([0-9]+)-([0-9]+)(.).html$"/exchange.php?cat_id=$1&page=$2 last;

rewrite"^/exchange-([0-9]+)(.).html$"/exchange.php?cat_id=$1 last;

}

7)PHPWind伪静态

rewrite ^(.)-htm-(.)$ $1.php?$2 last;

rewrite ^(.*)/simple/([a-z0-9_]+.html)$ $1/simple/index.php?$2 last;

8)SaBlog2.0伪静态

只带月份的归档

rewrite"^/date/([0-9]{6})/?([0-9]+)?/?$"/index.php?action=article&setdate=$1&page=$2 last;

无分类翻页

rewrite ^/page/([0-9]+)?/?$/index.php?action=article&page=$1 last;

分类

rewrite ^/category/([0-9]+)/?([0-9]+)?/?$/index.php?action=article&cid=$1&page=$2 last;

rewrite ^/category/([^/]+)/?([0-9]+)?/?$/index.php?action=article&curl=$1&page=$2 last;

归档、高级搜索

rewrite ^/(archives|search|article|links)/?$/index.php?action=$1 last;

全部评论、标签列表、引用列表 带分页

rewrite ^/(comments|tagslist|trackbacks|article)/?([0-9]+)?/?$/index.php?action=$1&page=$2 last;

tags

rewrite ^/tag/([^/]+)/?([0-9]+)?/?$/index.php?action=article&item=$1&page=$2 last;

文章

rewrite ^/archives/([0-9]+)/?([0-9]+)?/?$/index.php?action=show&id=$1&page=$2 last;

RSS rewrite ^/rss/([0-9]+)?/?$/rss.php?cid=$1 last;

rewrite ^/rss/([^/]+)/?$/rss.php?url=$1 last;

用户 rewrite ^/uid/([0-9]+)/?([0-9]+)?/?$/index.php?action=article&uid=$1&page=$2 last;

rewrite ^/user/([^/]+)/?([0-9]+)?/?$/index.php?action=article&user=$1&page=$2 last;

地图文件

rewrite sitemap.xml sitemap.php last;

自定义链接

rewrite ^(.*)/([0-9a-zA-Z-_]+)/?([0-9]+)?/?$ $1/index.php?action=show&alias=$2&page=$3 last;

9)SHOPEX伪静态

if(!-e $request_filename) {

rewrite ^/(.+.(html|xml|json|htm|php|jsp|asp|shtml))$/index.php?$1 last;

}

10)Typecho伪静态

if(-f $request_filename/index.html){

rewrite (.) $1/index.htmlbreak;

}

if(-f $request_filename/index.php){

rewrite (.) $1/index.php;

}

if(!-f $request_filename){

rewrite (.*)/index.php;

}

Nginx中的rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用。

rewrite伪静态重写的执行顺序:

(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

五、Apache伪静态配置和常用Rewrite伪静态规则演示集锦

Apache虚拟机配置及伪静态规则

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

1)编辑Apache的conf目录下的httpd.conf文件。

去除"# LoadModule rewrite_module modules/mod_rewrite.so"的注释,开启mod_rewrite.so模块支持。

去除"# Include conf/extra/httpd-vhosts.conf"的注释,引入虚拟机配置文件。

2) 编辑httpd-vhost.conf

<VirtualHost *:80>

#发生错误时将发送邮件

#ServerAdmin test@kevin.com

#文档根目录

DocumentRoot"/data/www/httpd"

#域名

ServerName www.kevin.com

#错误日志

ErrorLog"logs/error.log"

#访问日志

CustomLog"logs/access.log"

#配置rewrite相关选项

<Directory"/data/www/httpd">

#允许所有指令,这将允许.htaccess

AllowOverride All

#2.2的访问控制配置,先检查允许的条件,没有允许的全部禁止,中间只能有一个逗号不能有空格

#Order Allow,Deny

#Allow from All

#2.4的访问控制配置,效果等同以上

Require all granted

</Directory>

3) 修改.htaccess

#以下表示:如果存在目录或文件则直接访问,否则执行RewriteRule

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

#隐藏index.php

RewriteRule ^(.*)$ index.php/$1 [L]

4) 重启apache服务

Apache伪静态配置示例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

伪静态就是将原来动态化的页面址转换成为静态化的地址。

例如:

原访问地址:http://www.test.com/list.php?page=123&id=456

伪静态地址:http://www.test.com/list-123-456.html

操作方法:

1)首先确认Apache已经正确加载了mod_rewrite模块

检查httpd.conf中是否有LoadModule Rewrite_module modules/mod_Rewrite.so这段代码,如没有请加上。

2)策略配置。现有一个网站,根目录为/var/www/html,动态页面地址为/list.php?page=123&id=456,现在我们想要的效果是/list-123-456.html

2.1)使用httpd.conf来配置rewrite策略:

要使用httpd.conf文件来设置伪静态策略,可以直接在httpd.conf中写入如下代码,如果网站是配置在VirtualHost中,

则将这段代码加到对应的<VirtualHosthostname><VirtualHost>标签内:

<IfModule mod_rewrite.c>

#输入: list-123-456.html

#输出: list.php?page=123&id=456

RewriteEngine on

RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$1&id=$2

</IfModule>

添加完成后重启httpd服务后即可生效

2.2)使用.htaccess来配置rewrite策略

检查httpd.conf中的<Directory />标签配置,确认AllowOverride配置为All,这样才能启用.htaccess文件:

<Directory />

Options FollowSymLinks

AllowOverride All

</Directory>

检查httpd.conf中的AccessFileName参数,确认为.htaccess

AccessFileName .htaccess

在网站根目录下建立.htaccess文件,写入如下内容:

RewriteEngine on

RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3

保存后重启httpd服务即可生效

常见问题:

1)为何都按上面设置了却还是无法静态化?

答:很有可能是因为别的目录设置项覆盖了<Directory />标签内的选项,导致.htaccess文件没起作用。

这个问题一般出现在网站根目录的Directory标签中,在这个例子中,可以检查<Directroy"/var/www/html">标签内的AllowOverride参数是否设置为All。

2).htaccess文件放在网站根目录,那子目录也可以实现伪静态吗?

答:.htaccess默认对所在目录下所有子目录生效,但是如果子目录中也放置了.htaccess文件,则该子目录下的访问规则以子目录中的.htaccess文件为准。

Apache开启伪静态示例(修改"AllowOverride ALL",打开支持.htaccess伪静态文件的功能)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

伪静态只是改变了URL的显示形式,实际上还是网站页面还是动态页面。伪静态的页面后缀可以是html 、 htm 或者是目录格式等。那么为什么要用伪静态呢?

有两点原因:1是seo优化,伪静态有利于搜索引擎的收录,能够增加网站优化效果;2是url看起来简单,网站URL给人专业性。

1)加载Rewrite模块:

在conf目录下httpd.conf中找到

LoadModule rewrite_module modules/mod_rewrite.so

2)允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be “All”, “None”, or any combination of the keywords:

# Options FileInfo AuthConfig Limit

#

AllowOverride All

# 把 AllowOverride None 改为 AllowOverride All,重启一下apache服务器使配置生效,这样就支持.htaccess文件了。

3)Apache Rewrite模块的简单应用

Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。

3.1)请求跳转

目的是如果请求为.jsp文件,则跳转至其它域名访问。

例如:

访问www.clin003.com/a.php跳转至b.clin003.com/b.php网页,访问www.clin003.com/news/index.php跳转至b.clin003.com/news/index.php网页

注意:

不是使用HTML技术中的meta或者javascript方式,因为www.clin003.com/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。

修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容

RewriteEngine on

#开启Rewrite模块

RewriteRule (.*)\.php$ http://b.clin003.com/$1\.jsp [R=301,L,NC]

#截获所有.jsp请求,跳转到http://b.clin003.com/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写

3.2)域名跳转

如果请求为old.clin003.com下的所有URL,跳转至b.clin003.com

RewriteEngine on

#开启Rewrite模块

RewriteCond %{REMOTE_HOST} ^old.studenthome.cn$ [NC]

#针对host为old.clin003.com的主机做处理,^为开始字符,$为结尾字符

RewriteRule (.*) http://b.clin003.com/$1 [R=301,L,NC]

3.3)防盗链

如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容

RewriteEngine on

#开启Rewrite模块

RewriteCond %{HTTP_REFERER} !^$

#如果不是直接输入图片地址

RewriteCond %{HTTP_REFERER} !img.clin003.com$ [NC]

#且如果不是img.clin003.com所有子域名调用的

RewriteCond %{HTTP_REFERER} !img.clin003.com/(.*)$ [NC]

RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC]

RewriteCond %{HTTP_REFERER} !google.com [NC]

RewriteCond %{HTTP_REFERER} !google.cn [NC]

RewriteCond %{HTTP_REFERER} !baidu.com [NC]

RewriteCond %{HTTP_REFERER} !feedsky.com [NC]

RewriteRule (.*)\.(jpg|jpeg|jpe|gif|bmp|png|wma|mp3|wav|avi|mp4|flv|swf)$ http://clin003.com/err.jpg [R=301,L,NC]

#截获所有.jpg或.jpeg……请求,跳转到http://clin003.com/err.jpg提示错误的图片,注:该图片不能在原域名下,也不能在该.htaccess文件有效控制的文件夹中

对配置做几点补充说明:

L 表明当前规则是最后一条规则,停止分析以后重写

NC 不区分大小写

QSA 追加请求的字符串

^ 表示语句开始

$ 表示语句的结束

3.4)不需要定义.htaccess文件

在Apache2\conf\httpd.conf 最后一行添加

RewriteEngine On

RewriteRule ^(.*)-htm-(.*)$ $1.php?$2

Apache各种跳转(包括伪静态)的配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

1)404跳转:

#vim /etc/httpd/conf/httpd.conf

在虚拟主机配置里添加一行:ErrorDocument 404/404.html

2)301跳转:

将不带www的跳转到带www的:在根目录下新建.htaccess文件,写入:

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} ^manyi.cc [NC]

RewriteRule ^(.*)$ http://www.manyi.cc/$1 [L,R=301]

重定向到新域名:

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^(.*)$ http://www.manyi.cc/$1 [L,R=301]

3)在httpd.conf配置文件中配置:

<VirtualHost *:80>

ServerName manyi.cc

RedirectMatch permanent ^/(.*) http://www.manyi.cc/$1

</VirtualHost>

使用正则进行301伪静态配置:(将news.php?id=123这样的地址转向到news-123.html)

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^news-(.+)\.html$ news.php?id=$1

FastCGI加载PHP伪静态设置的注意事项

?

1

2

3

4

5

6

7

8

9

10

默认的"RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]"规则在apache fastcgi模式下会导致"No input file specified".

修改成

RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]

这样就好了,地址正常重写。

#php api模式,服务器能识别PATH_INFO

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

#php fastcgi模式 服务器不识别PATH_INFO

RewriteRule ^(.*)$ index.php [E=PATH_INFO:$1,QSA,PT,L]

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************

一、什么是伪静态

伪静态即是网站本身是动态网页如.php、.asp、.aspx等格式动态网页有时这类动态网页还跟"?"加参数来读取数据库内不同资料,伪静态就是做url重写操作(即rewrite)。很典型的案例即是discuz论坛系统,后台就有一个设置伪静态功能,开启伪静态后,动态网页即被转换重写成静态网页类型页面,通过浏览器访问地址和真的静态页面没区别。但是记住:做伪静态的前提就是服务器要支持伪静态重写URL Rewrite功能。

考虑搜索引擎优化(即SEO),将动态网页通过服务器处理成静态页面,如www.kevin.com/jk/fd.php?=12这样的动态网页处理成www.kevin.com/jk-fd-12.html这样格式静态页面,常见的论坛帖子页面,都是经过伪静态处理成静态页面格式html页面。由于网站所用的程序语言不易被发现,经过重写来伪静态来将动态网页的程序后缀变为html的静态页面格式。伪静态是一种可以把文件后缀改成任何可能的一种方法,比如如果想把php文件伪静态成html文件,这种配置相当简单的,后面会提到相应配置。

二、真静态、伪静态优点和缺点

真静态(html)优点;1)减少服务器对数据响应的负荷;2)加载不用调动数据库,响应速度快。

真静态缺点:1)维护不方便,每次都要手动生成;2)空间占用比较大,容易造成磁盘压力;3)生成的文件多,服务器对html文件的响应负担也较重。

伪静态(url重写)优点:1)可以方便的实现对化化引擎的优化,并且比生成静态更加方便;2)占空间比较小;3)首页每天都自动变化,不用维护。网站首页一般都有热点排行之类的,你可以设为,24小时排行,一周排行,再加上最新文章,最新点评等。这样首页天天是有变化的;4)便于广告的轮显。比如:你可以把 art1234.aspx,这个虚成n个页,如art_1234.aspx,news_1234.aspx,top_1234.aspx,在不同的页面放 不同的广告。总之是动态的,就可以随意动。

伪静态缺点:1)如果流量稍大一些使用伪静态就出现CPU使用超负荷,因为伪静态是用正则判断而不是真实地址,分辨到底显示哪个页面的责任也由直接指定转由CPU来判断了,所以CPU占有量的上升,确实是伪静态最大的弊病;2)伪静态效率不如生成html的,因为它不是真正意义上的静态页,所以每次请求都是要去读取数据库的信息(这个可以用缓存技术来补偿一下)。

三、真静态、伪静态原理与实现方案

1、伪静态

伪静态是相对于真静态而言的,就是把一些asp,php等结尾url通过apche或nginx的重写规则,变成以html一类的静态页面形式。伪静态不是真正的静态,它和动态地址一样要读取数据库。伪静态最主要的作用就是利于seo,百度spider(百度蜘蛛)喜欢抓取静态页面,可容易使百度spider陷入死循环;并发量高的时候会加大服务器的压力,所以用的时候要注意。

伪静态就是利用apche,nginx重写规则,对url地址重写实现的!伪静态实现原理:

1) Apache伪静态前提是要打开apache的重写模块 (即打开"LoadModule rewrite_module modules/mod_rewrite.so"这一行);

2) Nginx默认就支持伪静态;

伪静态有两种配置方式

1) 在配置虚拟主机的时候设置;

2) 在web根目录下创建一个.htaccess文件,在这个文件里面配置;

2、真静态

在网站设计中,纯粹HTML(标准通用标记语言下的一个应用)格式的网页通常被称为"静态网页",静态网页是标准的HTML文件,它的文件扩展名是.htm、.html,可以包含文本、图像、声音、FLASH动画、客户端脚本和ActiveX控件及JAVA小程序等。静态网页是网站建设的基础,早期的网站一般都是由静态网页制作的。静态网页是相对于动态网页而言,是指没有后台数据库、不含程序和不可交互的网页。静态网页相对更新起来比较麻烦,适用于一般更新较少的展示型网站。容易误解的是静态页面都是htm这类页面,实际上静态也不是完全静态,它也可以出现各种动态的效果,如GIF格式的动画、FLASH、滚动字幕等。

大型web项目优化中经常会考虑到使用真静态,这样在访问量大的时候,可以减少cpu的压力,但是会生成大量的文件占用网站的磁盘空间,可以写个php的脚本或用linux的计划任务进行删除。在用真静态的时候有的时候需要用到局部的动态化。

真静态实现方法

1)利用PHP模板生成静态页面;

2)使用PHP文件读写功能生成静态页面;

3)使用PHP输出控制函数缓存机制生成静态页面;

4)使用nosql从内存中读取内容(其实这个已经不算静态化了而是缓存);

memcached是键值一一对应,key默认最大不能超过128个字节,value默认大小是1M,因此1M大小满足大多数网页大小的存储。

真静态和伪静态的区别

1)是不是一个真正静态页面;

2)有没有和数据库或后台程序进行交互;

3)它们的应用场景和解决的问题不同;

4)用javascript:alert(document.lastModified)来判断是真静态还是伪静态;

真静态在apache和nginx上的区别与否

1)真静态在nginx上的运行速度比apache运行速度快;

2)nginx处理静态文件对于apache来说消耗的内存少;

伪静态在apache和nginx上的区别与否

1)本质上没有区别,两者都是根据正则匹配对应的url的重写。但是apache和nginx上的伪静态规则还是有点不同,在配置的时候要注意;

2)apache处理伪静态比nginx更有优势;

先来看个简单的小例子

?

1

2

3

4

5

6

7

8

跳转需求:

访问http://www.kevin.com/p/123456.html 跳转成 http://a.aa.com/p/123456

配置如下:

rewrite ^/p/(\d+).html http://www.kevin.com/p/$1 last;

解释说明:

\d是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。

四、Nginx伪静态配置和常用Rewrite伪静态规则演示集锦

nginx防盗链

?

1

2

3

4

5

6

7

location ~* \.(gif|jpg|png)$ {

valid_referers none blocked http://kevin.com:81;#允许访问的来源,即所有来自http://kevin.com:81的gif|jpg|png结尾的文件

if($invalid_referer) {

rewrite ^/ http://kevin.com:81/c2_.html;

#return 403;

}

}

伪静态重写

?

1

2

3

4

5

location / {

rewrite c(\d+)_(.*).html/index.php?c=user&m=index&id=$1&title=$2 last;

root/usr/share/nginx/html/sta;

index index.html index.htm index.php;

}

对于生成大量的大量html文件,推荐使用rsync工具进行批量删除,做法如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

1) 建立一个空目录

# mkdir -p /root/kevin/tmp/

2) 确认需要清空的目标目录(即需要删除的大量html文件所在的目录),比如/root/kevin/tmp1/

3)使用rsync同步删除(注意目录后面的“/”),整体效率会快一个数量级的样子。

# rsync --delete-before -a -H /root/kevin/tmp/ /root/kevin/tmp1/

选项说明:

–delete-before 接收者在传输之前进行删除操作

–progress 在传输时显示传输过程

-a 归档模式,表示以递归方式传输文件,并保持所有文件属性

-H 保持硬连接的文件

-v详细输出模式

-stats 给出某些文件的传输状态

如下操作可以把某个超大文件删掉,比如删除/mnt/ops/web.html文件

1)建立空文件/mnt/ops/html.txt

2)# rsync --delete-before -a -H -v --progress --stats /mnt/ops/html.txt /mnt/ops/web.html

3)这样置为空后就可以快速删掉

原理:

把文件系统的目录与书籍的目录做类比,rm删除内容时,将目录的每一个条目逐个删除(unlink),需要循环重复操作很多次;

rsync删除内容时,建立好新的空目录,替换掉老目录,基本没开销。

测试大文件删除

1)生成文件

# for i in $(seq 1 500000); do echo test >>/opt/test/$i.txt; done

测试结果:

10万文件rm删除第一次11s左右,第二次9s左右;rsync测试两次9s左右

50万文件rm删除报错;rsync测试两次一个5分左右,一个4分左右

rm删除命令

# time rm -f /opt/test/*.txt

rsync命令删除

# mkdir /opt/test1

# rsync --delete-before -a -H -v --progress --stats /opt/test1/ /opt/test/

nginx php thinkphp5 伪静态配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

server {

listen 80;

server_name 127.0.0.1 localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

if(!-e $request_filename) {

#一级目录

#rewrite ^/(.*)$ /index.php?s=$1 last;

#二级目录

rewrite ^/TWcloud/public/(.*)$/TWcloud/public/index.php?s=$1 last;

break;

}

root/Applications/MxSrvs/www;

index index.html index.htm index.php;

}

location ~ \.php {#去掉$

root/Applications/MxSrvs/www;

fastcgi_pass 127.0.0.1:10080;

fastcgi_index index.php;

fastcgi_split_path_info ^(.+\.php)(.*)$;#增加这一句

fastcgi_param PATH_INFO $fastcgi_path_info;#增加这一句

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

thinkphp 5.0的nginx伪静态规则 (踩过坑点,经测试实现)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

server {

listen 80;

server_name www.kevin.com;

root/data/www/web;

location / {

index index.html index.htm index.php default.php;

#重点就是加入下面这个if

if(!-e $request_filename){

rewrite ^(.*)$/index.php?s=$1 last;break;

}

}

location ~ .php(.*)$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.html;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_split_path_info ^(.+.php)(.*)$;

fastcgi_param PATH_INFO $fastcgi_path_info;

include fastcgi_params;

}

}

nginx伪静态配置小案例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

场景一:

http://www.abc.com/index.php/front/index/index

重写成

http://www.abc.com/a.html

场景二:

把下面带参数的第1、2个url解析成第3个url

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/data/www/web;

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/indexlast;

#场景二的规则

#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是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。

nginx配置伪静态的Rewrite重写的正则使用说明

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

正则表达式匹配 :

~ 为区分大小写的匹配

~* 不区分大小写的匹配(匹配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以外的任意字符

文件及目录匹配,其中:

-f和!-f 用来判断是否存在文件

-d和!-d 用来判断是否存在目录

-e和!-e 用来判断是否存在文件或目录

-x和!-x 用来判断文件是否可执行

flag标记有:

last 相当于Apache里的[L]标记,表示完成rewrite

break终止匹配, 不再匹配后面的规则

redirect 返回302临时重定向 地址栏会显示跳转后的地址

permanent 返回301永久重定向 地址栏会显示跳转后的地址

$args 此变量与请求行中的参数相等

$content_length 等于请求行的“Content_Length”的值。

$content_type 等同与请求头部的”Content_Type”的值

$document_root 等同于当前请求的root指令指定的值

$document_uri 与 $uri 一样

$host 与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样

$limit_rate 允许限制的连接速率

$request_method 等同于request的method,通常是“GET”或“POST”

$remote_addr 客户端ip

$remote_port 客户端port

$remote_user 等同于用户名,由ngx_http_auth_basic_module认证

$request_filename 当前请求的文件的路径名,由root或alias和URI request组合而成

$request_body_file

$request_uri 含有参数的完整的初始URI

$query_string 与 $args一样

$server_protocol 等同于request的协议,使用“HTTP/1.0”或“HTTP/1.1”

$server_addr request 到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。

$server_name 请求到达的服务器名

$server_port 请求到达的服务器的端口号

$uri 等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index

nginx伪静态配置案例集锦

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

nginx里使用伪静态是直接在nginx.conf 中写规则的,并不需要像apache要开启写模块(mod_rewrite)才能进行伪静态。

nginx只需要打开nginx.conf配置文件,在server里面写需要的规则即可。

1)配置案例1

server {

listen 80;

server_name www.kevin.com;

index index.html index.htm index.php;

rewrite ^/wangla.html$ http://www.baidu.com/ permanent;

rewrite ^/(\d+).html$ http://www.qq.com/ permanent;

rewrite ^/(\w+).html$ http://wd.gyyx.cn/index_wd_v5.htm permanent;

}

以上添加了几条重写规则

访问www.kevin.com/wangla.html跳转到百度

访问www.kevin.com/纯数字至少一个数字.html跳转到QQ官网

访问www.kevin.com/匹配字母或数字或下划线组合.html 跳转到问道官网。

2)配置案例2

server {

listen 80;

server_name bbs.jb51.net;

index index.html index.htm index.php;

root/home/www/bbs;

error_page 404/404.htm;#配置404错误页面

location ~ .*.(php|php5)?$ {

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

#下面就是伪静态了

location /{

rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;

}

access_log access_log off;

}

然后重启nginx服务器伪静态就生效了,这种维护起来很是不方便我们可以把它写在外部文件如bbs_nginx.conf中

/home/www/bbs目录下创建bbs_nginx.conf文件并写入以下代码:

location /{

rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;

}

然后在上面的代码后面加上如下代码:

include/home/www/bbs/bbs_nginx.conf;

这样网站根目录中的bbs_nginx.conf伪静态规则,即可实现单独管理。

下面是一个实例:

在使用.htaccess文件的目录下新建一个.htaccess文件,如下面一个Discuz论坛目录:

# vim /var/www/html/jb51/bbs/.htaccess

rewrite ^(.*)/archiver/((fid|tid)-[w-]+.html)$ $1/archiver/index.php?$2 last;

rewrite ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 last;

rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;

rewrite ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 last;

rewrite ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 last;

rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last;

接着修改nginx配置文件:

# vim /etc/nginx/nginx.conf

在需要添加伪静态的虚拟主机的server{}中引入.htaccess文件:

include/var/www/html/jb51/bbs/.htaccess;

最后重新加载nginx配置文件:

# /etc/init.d/nginx reload

3)下面是一些rewrite配置集锦,供运维参考:

=======================================================================================

rewrite"^/(.{6})(\d{3})(.+)/php/"http://www.kevin.com/qq$2.apkbreak;

中间用到了{6}指前面的字符得复6次

结合PHP的例子

if(!-d $request_filename) {

rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$/index.php?namespace=user&controller=$1&action=$2&$3 last;

rewrite ^/([a-z-A-Z]+)/?$/index.php?namespace=user&controller=$1 last;

break;

多目录转成参数。

abc.domian.com/sort/2=> abc.domian.com/index.php?act=sort&name=abc&id=2

配置如下:

if($host ~* (.*)\.domain\.com) {

set$sub_name $1;

rewrite ^/sort\/(\d+)\/?$/index.php?act=sort&cbash functions">id=$1 last;

}

目录对换

/123456/xxxx->/xxxx?id=123456

配置如下:

rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;

例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:

if($http_user_agent ~ MSIE) {

rewrite ^(.*)$/nginx-ie/$1break;

}

目录自动加"/"

if(-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

禁止htaccess

location ~/\.ht {

deny all;

}

禁止多个目录

location ~ ^/(cron|templates)/ {

deny all;

break;

}

禁止以/data开头的文件

可以禁止/data/下多级目录下.log.txt等请求;

location ~ ^/data{

deny all;

}

禁止单个目录

不能禁止.log.txt能请求

location/searchword/cron/{

deny all;

}

禁止单个文件

location ~/data/sql/data.sql {

deny all;

}

给favicon.ico和robots.txt设置过期时间;

这里为favicon.ico为99 天,robots.txt为7天并不记录404错误日志

location ~(favicon.ico) {

log_not_found off;

expires 99d;

break;

}

location ~(robots.txt) {

log_not_found off;

expires 7d;

break;

}

设定某个文件的过期时间;这里为600秒,并不记录访问日志

location ^~/html/scripts/loadhead_1.js {

access_log off;

root/opt/lampp/htdocs/web;

expires 600;

break;

}

文件反盗链并设置过期时间

这里的return412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求

"rewrite ^/ http://leech.c1gstudio.com/leech.gif;"显示一张防盗链图片

"access_log off;"不记录访问日志,减轻压力

"expires 3d"所有文件3天的浏览器缓存

location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {

valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;

if($invalid_referer) {

rewrite ^/ http://leech.c1gstudio.com/leech.gif;

return412;

break;

}

access_log off;

root/opt/lampp/htdocs/web;

expires 3d;

break;

}

只充许固定ip访问网站,并加上密码

root/opt/htdocs/www;

allow 218.197.16.14;

allow 22.133.11.22;

allow 21.112.69.14;

deny all;

auth_basic"C1G_ADMIN";

auth_basic_user_file htpasswd;

将多级目录下的文件转成一个文件,增强seo效果

/job-123-456-789.html 指向/job/123/456/789.html

配置如下:

rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$/job/$1/$2/jobshow_$3.html last;

--------------------------------------------------------------------------------

将根目录下某个文件夹指向2级目录

/shanghaijob/指向/area/shanghai/

如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/

配置如下:

rewrite ^/([0-9a-z]+)job/(.*)$/area/$1/$2 last;

上面例子有个问题是访问/shanghai时将不会匹配

rewrite ^/([0-9a-z]+)job$/area/$1/ last;

rewrite ^/([0-9a-z]+)job/(.*)$/area/$1/$2 last;

这样/shanghai也可以访问了,但页面中的相对链接无法使用,

如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。

那加上自动跳转也是不行的!

(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果。

if(-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

知道原因后就好办了,手动跳转吧

rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;

rewrite ^/([0-9a-z]+)job/(.*)$/area/$1/$2 last;

--------------------------------------------------------------------------------

文件和目录不存在的时候重定向:

if(!-e $request_filename) {

proxy_pass http://127.0.0.1/;

}

域名跳转

server {

listen 80;

server_name jump.c1gstudio.com;

index index.html index.htm index.php;

root/opt/lampp/htdocs/www;

rewrite ^/ http://www.c1gstudio.com/;

access_log off;

}

多域名转向

server_name http://www.c1gstudio.com/ http://www.c1gstudio.net/;

index index.html index.htm index.php;

root/opt/lampp/htdocs;

if($host ~"c1gstudio\.net") {

rewrite ^(.*) http://www.c1gstudio.com$1/ permanent;

}

三级域名跳转

if($http_host ~*"^(.*)\.i\.c1gstudio\.com$") {

rewrite ^(.*) http://top.yingjiesheng.com$1/;

break;

}

域名镜向

server

{

listen 80;

server_name mirror.c1gstudio.com;

index index.html index.htm index.php;

root/opt/lampp/htdocs/www;

rewrite ^/(.*) http://www.c1gstudio.com/$1 last;

access_log off;

}

某个子目录作镜向

location ^~/zhaopinhui{

rewrite ^.+ http://zph.c1gstudio.com/ last;

break;

}

discuz ucenter home (uchome) rewrite伪静态配置

rewrite ^/(space|network)-(.+)\.html$ /$1.php?rewrite=$2 last;

rewrite ^/(space|network)\.html$ /$1.php last;

rewrite ^/([0-9]+)$/space.php?uid=$1 last;

discuz 7 rewrite伪静态配置

rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;

rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;

rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\=$4&page=$3 last;

rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;

rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;

rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;

给discuz某版块单独配置域名

server_name bbs.c1gstudio.com news.c1gstudio.com;

location = / {

if($http_host ~ news\.c1gstudio.com$) {

rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;

break;

}

}

discuz ucenter 头像 rewrite 优化

location ^~/ucenter{

location ~ .*\.php?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

location/ucenter/data/avatar{

log_not_found off;

access_log off;

location ~ /(.*)_big\.jpg$ {

error_page 404/ucenter/images/noavatar_big.gif;

}

location ~ /(.*)_middle\.jpg$ {

error_page 404/ucenter/images/noavatar_middle.gif;

}

location ~ /(.*)_small\.jpg$ {

error_page 404/ucenter/images/noavatar_small.gif;

}

expires 300;

break;

}

}

jspace rewrite伪静态配置

location ~ .*\.php?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

location ~* ^/index.php/

{

rewrite ^/index.php/(.*)/index.php?$1break;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

Nginx伪静态配置案例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

公司域名bo.kevin.com下有多个子项目目录结构大致是bo.kevin.com/own/xys|bo.kevin.com/2017/abc|bo.kevin.com/2018/def有二级也有三级目录,

应开发需求某项目访问地址是:bo.kevin.com/own/xys/index.php/admin/login需要把index.php隐藏为bo.kevin.com/own/xys/index/admin/login进行访问。

设定以下三种场景:

场景一

将 http://www.abc.com/index.php/front/index/index

重写成 http://www.abc.com/a.html

场景二

将 http://www.abc.com/index.php/front/index/parse?name=itboy&age=18

重写成 http://www.abc.com/parse-itboy-18.html

场景三(同一域名下,需要匹配随时新增的二三级目录,并隐藏index.php的.php后缀)

将 http://bo.kevin.com/own/xys/index.php/admin/login以及 http://bo.kevin.com/2018/gdhp/index.php/login

重写成 http://bo.kevin.com/own/xys/index/admin/login以及 http://bo.kevin.com/2018/gdhp

建议在nginx/conf目录下新建rewrite.conf配置文件中编写伪静态规则,写完后在域名.conf文件中插入rewrite.conf文件即可(用include rewrite.conf插入)。

nginx配置文件如下:

server

{

listen 80;

server_name bo.kevin.com;

index index.html index.php index.htm index.php default.html default.htm default.php;

root/var/www/apps/bo.kevin.com;

include rewrite.conf;

include none.conf;

error_page 502/502.html;

includeenable-php-pathinfo.conf;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

location ~ .*\.(js|css)?$

{

expires 12h;

}

location ~ /\.

{

deny all;

}

access_log/var/www/wwwlogs/bo.kevin.com.log;

error_log/var/www/wwwlogs/error.bo.kevin.com.log;

}

rewrite.conf文件内容如下:

location /{

#场景一

#http://www.abc.com/index.php/front/index/index 变成 http://www.abc.com/a.html

rewrite a.html/index.php/front/index/indexlast;

#场景二

#http://www.abc.com/index.php/front/index/parse?name=itboy&age=18 变成 http://www.abc.com/parse-itboy-18.html

rewrite parse-(\w+)-(\d+).html/index.php/front/index/parse/name/$1/age/$2 last;

#场景三

#http://bo.kevin.com/own/xys/index.php/admin/login 以及 http://bo.kevin.com/2018/gdhp/index.php/login

#变成 http://bo.kevin.com/own/xys/index/admin/login 以及 http://bo.kevin.com/2018/gdhp/index/login

rewrite ^/(\w+)/(\w+)/(.*)$ /$1/$2/index.php?s=$3 last;#针对own目录伪静态规则,$1对应(\w+)部分,$2对应第二个(\w+)部分,$3对应(.*)部分,$代表直至最后

rewrite ^/(\d+)/(\w+)/(.*)$ /$1/$2/index.php?s=$3 last;#针对后期的2018下的子项目伪静态规则

}

说明:

其实都是很简单的对号入座原理而已,拿场景二来说明,第一个正则(\w+)对应的就是$1,第二个正则(\d+)对应的就是$2,

另外,\w是数字字母下划线的意思,\d是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。

Nginx上支持.htaccess伪静态的配置实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# vim /usr/local/nginx/conf/vhost/web.conf

........

include/var/www/web/.htaccess

# vim /var/www/web/.htaccess

rewrite ^/show-([0-9]+)-([0-9]+)\.html$/index.php?action=show&id=$1&page=$2;

rewrite ^/category-([0-9]+)-([0-9]+)\.html$/index.php?action=index&cid=$1&page=$2;

rewrite ^/archives-([0-9]+)-([0-9]+)\.html$/index.php?action=index&setdate=$1&page=$2;

rewrite ^/(archives|search|reg|login|index|links)\.html$/index.php?action=$1;

rewrite ^/(comments|tagslist|trackbacks|index)-([0-9]+)\.html$/index.php?action=$1&page=$2;

if($host !='www.shibo.com') {

rewrite ^/(.*)$ http://www.shibo.com/$1 permanent;

}

error_page 404 http://www.shibo.com/;

# /usr/local/nginx/sbin/nginx -s reload

解决nginx配置伪静态(去除框架的Index.php)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

在nginx.conf中Location/{}中加上下面这个if判断就可以了:

location /{

if(!-e $request_filename) {

rewrite ^(.*)$/index.php?s=$1 last;break;

}

}

===============================================================================

如下面一个配置

if(!-e $request_filename) {

rewrite ^/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\.html/index.php?m=$1&c=$2&a=$3&$4=$5&$6=$7 last;

break;

}

Nginx 常用伪静态配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

访问 http://www.kevin.com/sort/15.html -> http://www.kevin.com/1.php?id=15

location / {

root/usr/share/nginx/html/1;

index index.html;

rewrite/sort/(.*)\.html/1.php?id=$1 last;

}

================================================================================

再如下面一个配置

server {

listen 80 default_server;

server_name _;

location / {

root/usr/share/nginx/html;

index index.html index.htm;

rewrite ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3;

}

}

策略:RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3

请求路径:http://www.abc.com/list-123-456.html

以上策略分成两段:

第一段是使用正则表达式去匹配请求访问的路径,第二段是将匹配后的参数转化为真实访问的路径。

策略执行时:^(.*)list-([0-9]+)-([0-9]+)\.html$ 与/list-123-456.html 这个字符串进行匹配:

^和$字符分别代表了匹配输入字符串的开始和结束;

()中的匹配到的内容会被按顺序分配到变量$1 $2 $3中;

.*匹配任意字符串,且长度从0个到多个,故$1值为/;

[0-9]+匹配字符0-9,长度1个到多个,故$2和$3分别是123和456;

所以最后真实访问的动态地址为/list.php?page=123&id=456

================================================================================

再如下面一个配置

1)/a/b?c=d => index.php?_a=a&_m=b&c=d

2)/xxx/detail-yyy.html => index.php?_a=xxx&_m=detail&id=yyy

配置如下:

server {

listen 80;

server_name my.xh5.com;

location / {

root/mnt/hgfs/web/my.xueh5.com/src/;

index index.html index.htm index.php;

if($args ~"^(.*)$"){

set$rule_0 1$rule_0;

set$bref_1 $1;

}

if($rule_0 ="1"){

rewrite ^([0-9a-zA-Z]*)/detail-([0-9]*)\.html$/index.php?_a=$1&_m=detail&id=$2&$bref_1 last;

rewrite ^/([0-9a-zA-Z]*)/([0-9a-zA-Z.]*)$/index.php?_a=$1&_m=$2&$bref_1 last;

rewrite ^/([0-9a-zA-Z]+)$/index.php?_a=$1&_m=index&$bref_1 last;

rewrite ^/$/index.php?_a=index&_m=index&$bref_1 last;

}

}

location ~ \.php$ {

root/mnt/hgfs/web/my.xueh5.com/src/;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

Nginx常用伪静态规则小总结

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

1)WordPress伪静态

if(-f $request_filename/index.html){

rewrite (.) $1/index.htmlbreak;

}

if(-f $request_filename/index.php){

rewrite (.) $1/index.php;

}

if(!-f $request_filename){

rewrite (.*)/index.php;

}

2)PHPCMS伪静态

rewrite ^/caipu-([0-9]+)-([0-9]+)-([0-9]+).html/index.php?m=content&c=index&a=show&catbash functions">id=$2&page=$3 last;

rewrite ^/content-([0-9]+)-([0-9]+)-([0-9]+).html/index.php?m=content&c=index&a=show&catbash functions">id=$2&page=$3 last;

rewrite ^/list-([0-9]+)-([0-9]+).html/index.php?m=content&c=index&a=lists&catid=$1&page=$2 last;

rewrite ^/tag-([^.])-([0-9]+)-([0-9]+).html/index.php?m=content&c=tag&catid=$2&tag=$1&page=$3 last;

rewrite ^/comment-([0-9]+)-([0-9]+)-([0-9]+).html/index.php?m=comment&c=index&a=init&commentid=content_$1-$2-$3 last;

rewrite ^/([^.]).html/index.php?m=member&c=index&a=$1 last;

3)DEDECMS伪静态

rewrite"^/index.html$"/index.php last;

rewrite"^/list-([0-9]+).html$"/plus/list.php?tid=$1 last;

rewrite"^/list-([0-9]+)-([0-9]+)-([0-9]+).html$"/plus/list.php?tid=$1&totalresult=$2&PageNo=$3 last;

rewrite"^/view-([0-9]+)-1.html$"/plus/view.php?arcID=$1 last;

rewrite"^/view-([0-9]+)-([0-9]+).html$"/plus/view.php?aid=$1&pageno=$2 last;

rewrite"^/tags.html$"/tags.php last;

rewrite"^/tag-([0-9]+)-([0-9]+).html$"/tags.php?/$1/$2/ last;

4)Discuz7伪静态

rewrite ^/archiver/((fid|tid)-[\w-]+.html)$/archiver/index.php?$1 last;

rewrite ^/forum-([0-9]+)-([0-9]+).html$/forumdisplay.php?fid=$1&page=$2 last;

rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$/viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;

rewrite ^/space-(username|uid)-(.+).html$/space.php?$1=$2 last;

rewrite ^/tag-(.+).html$/tag.php?name=$1 last;

5)DiscuzX伪静态

rewrite ^([^.])/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last;

rewrite ^([^.])/article-([0-9]+)-([0-9]+).html$ $1/portal.php?mod=view&aid=$2&page=$3 last;

rewrite ^([^.])/forum-(\w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

rewrite ^([^.])/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;

rewrite ^([^.])/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last;

rewrite ^([^.])/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last;

rewrite ^([^.]*)/([a-z]+)-(.+).html$ $1/$2.php?rewrite=$3 last;

if(!-e $request_filename) {

return404;

}

6)ECSHOP伪静态

if(!-e $request_filename)

{

rewrite"^/index.html"/index.php last;

rewrite"^/category$"/index.php last;

rewrite"^/feed-c([0-9]+).xml$"/feed.php?cat=$1 last;

rewrite"^/feed-b([0-9]+).xml$"/feed.php?brand=$1 last;

rewrite"^/feed.xml$"/feed.php last;

rewrite"^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-])-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 last;

rewrite"^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-])(.).html$"/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 last;

rewrite"^/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/category.php?id=$1&brand=$2&page=$3&sort=$4&order=$5 last;

rewrite"^/category-([0-9]+)-b([0-9]+)-([0-9]+)(.).html$"/category.php?id=$1&brand=$2&page=$3 last;

rewrite"^/category-([0-9]+)-b([0-9]+)(.).html$"/category.php?id=$1&brand=$2 last;

rewrite"^/category-([0-9]+)(.).html$"/category.php?id=$1 last;

rewrite"^/goods-([0-9]+)(.).html"/goods.php?id=$1 last;

rewrite"^/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/article_cat.php?id=$1&page=$2&sort=$3&order=$4 last;

rewrite"^/article_cat-([0-9]+)-([0-9]+)(.).html$"/article_cat.php?id=$1&page=$2 last;

rewrite"^/article_cat-([0-9]+)(.).html$"/article_cat.php?id=$1 last;

rewrite"^/article-([0-9]+)(.).html$"/article.php?id=$1 last;

rewrite"^/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+).html"/brand.php?id=$1&cat=$2&page=$3&sort=$4&order=$5 last;

rewrite"^/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.).html"/brand.php?id=$1&cat=$2&page=$3 last;

rewrite"^/brand-([0-9]+)-c([0-9]+)(.).html"/brand.php?id=$1&cat=$2 last;

rewrite"^/brand-([0-9]+)(.).html"/brand.php?id=$1 last;

rewrite"^/tag-(.).html"/search.php?keywords=$1 last;

rewrite"^/snatch-([0-9]+).html$"/snatch.php?id=$1 last;

rewrite"^/group_buy-([0-9]+).html$"/group_buy.php?act=view&id=$1 last;

rewrite"^/auction-([0-9]+).html$"/auction.php?act=view&id=$1 last;

rewrite"^/exchange-id([0-9]+)(.).html$"/exchange.php?id=$1&act=view last;

rewrite"^/exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/exchange.php?cat_bash functions">sort=$5&order=$6 last;

rewrite ^/exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$"/exchange.php?cat_bash functions">sort=$3&order=$4 last;

rewrite"^/exchange-([0-9]+)-([0-9]+)(.).html$"/exchange.php?cat_id=$1&page=$2 last;

rewrite"^/exchange-([0-9]+)(.).html$"/exchange.php?cat_id=$1 last;

}

7)PHPWind伪静态

rewrite ^(.)-htm-(.)$ $1.php?$2 last;

rewrite ^(.*)/simple/([a-z0-9_]+.html)$ $1/simple/index.php?$2 last;

8)SaBlog2.0伪静态

只带月份的归档

rewrite"^/date/([0-9]{6})/?([0-9]+)?/?$"/index.php?action=article&setdate=$1&page=$2 last;

无分类翻页

rewrite ^/page/([0-9]+)?/?$/index.php?action=article&page=$1 last;

分类

rewrite ^/category/([0-9]+)/?([0-9]+)?/?$/index.php?action=article&cid=$1&page=$2 last;

rewrite ^/category/([^/]+)/?([0-9]+)?/?$/index.php?action=article&curl=$1&page=$2 last;

归档、高级搜索

rewrite ^/(archives|search|article|links)/?$/index.php?action=$1 last;

全部评论、标签列表、引用列表 带分页

rewrite ^/(comments|tagslist|trackbacks|article)/?([0-9]+)?/?$/index.php?action=$1&page=$2 last;

tags

rewrite ^/tag/([^/]+)/?([0-9]+)?/?$/index.php?action=article&item=$1&page=$2 last;

文章

rewrite ^/archives/([0-9]+)/?([0-9]+)?/?$/index.php?action=show&id=$1&page=$2 last;

RSS rewrite ^/rss/([0-9]+)?/?$/rss.php?cid=$1 last;

rewrite ^/rss/([^/]+)/?$/rss.php?url=$1 last;

用户 rewrite ^/uid/([0-9]+)/?([0-9]+)?/?$/index.php?action=article&uid=$1&page=$2 last;

rewrite ^/user/([^/]+)/?([0-9]+)?/?$/index.php?action=article&user=$1&page=$2 last;

地图文件

rewrite sitemap.xml sitemap.php last;

自定义链接

rewrite ^(.*)/([0-9a-zA-Z-_]+)/?([0-9]+)?/?$ $1/index.php?action=show&alias=$2&page=$3 last;

9)SHOPEX伪静态

if(!-e $request_filename) {

rewrite ^/(.+.(html|xml|json|htm|php|jsp|asp|shtml))$/index.php?$1 last;

}

10)Typecho伪静态

if(-f $request_filename/index.html){

rewrite (.) $1/index.htmlbreak;

}

if(-f $request_filename/index.php){

rewrite (.) $1/index.php;

}

if(!-f $request_filename){

rewrite (.*)/index.php;

}

Nginx中的rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用。

rewrite伪静态重写的执行顺序:

(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

五、Apache伪静态配置和常用Rewrite伪静态规则演示集锦

Apache虚拟机配置及伪静态规则

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

1)编辑Apache的conf目录下的httpd.conf文件。

去除"# LoadModule rewrite_module modules/mod_rewrite.so"的注释,开启mod_rewrite.so模块支持。

去除"# Include conf/extra/httpd-vhosts.conf"的注释,引入虚拟机配置文件。

2) 编辑httpd-vhost.conf

<VirtualHost *:80>

#发生错误时将发送邮件

#ServerAdmin test@kevin.com

#文档根目录

DocumentRoot"/data/www/httpd"

#域名

ServerName www.kevin.com

#错误日志

ErrorLog"logs/error.log"

#访问日志

CustomLog"logs/access.log"

#配置rewrite相关选项

<Directory"/data/www/httpd">

#允许所有指令,这将允许.htaccess

AllowOverride All

#2.2的访问控制配置,先检查允许的条件,没有允许的全部禁止,中间只能有一个逗号不能有空格

#Order Allow,Deny

#Allow from All

#2.4的访问控制配置,效果等同以上

Require all granted

</Directory>

3) 修改.htaccess

#以下表示:如果存在目录或文件则直接访问,否则执行RewriteRule

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

#隐藏index.php

RewriteRule ^(.*)$ index.php/$1 [L]

4) 重启apache服务

Apache伪静态配置示例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

伪静态就是将原来动态化的页面址转换成为静态化的地址。

例如:

原访问地址:http://www.test.com/list.php?page=123&id=456

伪静态地址:http://www.test.com/list-123-456.html

操作方法:

1)首先确认Apache已经正确加载了mod_rewrite模块

检查httpd.conf中是否有LoadModule Rewrite_module modules/mod_Rewrite.so这段代码,如没有请加上。

2)策略配置。现有一个网站,根目录为/var/www/html,动态页面地址为/list.php?page=123&id=456,现在我们想要的效果是/list-123-456.html

2.1)使用httpd.conf来配置rewrite策略:

要使用httpd.conf文件来设置伪静态策略,可以直接在httpd.conf中写入如下代码,如果网站是配置在VirtualHost中,

则将这段代码加到对应的<VirtualHosthostname><VirtualHost>标签内:

<IfModule mod_rewrite.c>

#输入: list-123-456.html

#输出: list.php?page=123&id=456

RewriteEngine on

RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$1&id=$2

</IfModule>

添加完成后重启httpd服务后即可生效

2.2)使用.htaccess来配置rewrite策略

检查httpd.conf中的<Directory />标签配置,确认AllowOverride配置为All,这样才能启用.htaccess文件:

<Directory />

Options FollowSymLinks

AllowOverride All

</Directory>

检查httpd.conf中的AccessFileName参数,确认为.htaccess

AccessFileName .htaccess

在网站根目录下建立.htaccess文件,写入如下内容:

RewriteEngine on

RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3

保存后重启httpd服务即可生效

常见问题:

1)为何都按上面设置了却还是无法静态化?

答:很有可能是因为别的目录设置项覆盖了<Directory />标签内的选项,导致.htaccess文件没起作用。

这个问题一般出现在网站根目录的Directory标签中,在这个例子中,可以检查<Directroy"/var/www/html">标签内的AllowOverride参数是否设置为All。

2).htaccess文件放在网站根目录,那子目录也可以实现伪静态吗?

答:.htaccess默认对所在目录下所有子目录生效,但是如果子目录中也放置了.htaccess文件,则该子目录下的访问规则以子目录中的.htaccess文件为准。

Apache开启伪静态示例(修改"AllowOverride ALL",打开支持.htaccess伪静态文件的功能)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

伪静态只是改变了URL的显示形式,实际上还是网站页面还是动态页面。伪静态的页面后缀可以是html 、 htm 或者是目录格式等。那么为什么要用伪静态呢?

有两点原因:1是seo优化,伪静态有利于搜索引擎的收录,能够增加网站优化效果;2是url看起来简单,网站URL给人专业性。

1)加载Rewrite模块:

在conf目录下httpd.conf中找到

LoadModule rewrite_module modules/mod_rewrite.so

2)允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be “All”, “None”, or any combination of the keywords:

# Options FileInfo AuthConfig Limit

#

AllowOverride All

# 把 AllowOverride None 改为 AllowOverride All,重启一下apache服务器使配置生效,这样就支持.htaccess文件了。

3)Apache Rewrite模块的简单应用

Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。

3.1)请求跳转

目的是如果请求为.jsp文件,则跳转至其它域名访问。

例如:

访问www.clin003.com/a.php跳转至b.clin003.com/b.php网页,访问www.clin003.com/news/index.php跳转至b.clin003.com/news/index.php网页

注意:

不是使用HTML技术中的meta或者javascript方式,因为www.clin003.com/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。

修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容

RewriteEngine on

#开启Rewrite模块

RewriteRule (.*)\.php$ http://b.clin003.com/$1\.jsp [R=301,L,NC]

#截获所有.jsp请求,跳转到http://b.clin003.com/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写

3.2)域名跳转

如果请求为old.clin003.com下的所有URL,跳转至b.clin003.com

RewriteEngine on

#开启Rewrite模块

RewriteCond %{REMOTE_HOST} ^old.studenthome.cn$ [NC]

#针对host为old.clin003.com的主机做处理,^为开始字符,$为结尾字符

RewriteRule (.*) http://b.clin003.com/$1 [R=301,L,NC]

3.3)防盗链

如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容

RewriteEngine on

#开启Rewrite模块

RewriteCond %{HTTP_REFERER} !^$

#如果不是直接输入图片地址

RewriteCond %{HTTP_REFERER} !img.clin003.com$ [NC]

#且如果不是img.clin003.com所有子域名调用的

RewriteCond %{HTTP_REFERER} !img.clin003.com/(.*)$ [NC]

RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC]

RewriteCond %{HTTP_REFERER} !google.com [NC]

RewriteCond %{HTTP_REFERER} !google.cn [NC]

RewriteCond %{HTTP_REFERER} !baidu.com [NC]

RewriteCond %{HTTP_REFERER} !feedsky.com [NC]

RewriteRule (.*)\.(jpg|jpeg|jpe|gif|bmp|png|wma|mp3|wav|avi|mp4|flv|swf)$ http://clin003.com/err.jpg [R=301,L,NC]

#截获所有.jpg或.jpeg……请求,跳转到http://clin003.com/err.jpg提示错误的图片,注:该图片不能在原域名下,也不能在该.htaccess文件有效控制的文件夹中

对配置做几点补充说明:

L 表明当前规则是最后一条规则,停止分析以后重写

NC 不区分大小写

QSA 追加请求的字符串

^ 表示语句开始

$ 表示语句的结束

3.4)不需要定义.htaccess文件

在Apache2\conf\httpd.conf 最后一行添加

RewriteEngine On

RewriteRule ^(.*)-htm-(.*)$ $1.php?$2

Apache各种跳转(包括伪静态)的配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

1)404跳转:

#vim /etc/httpd/conf/httpd.conf

在虚拟主机配置里添加一行:ErrorDocument 404/404.html

2)301跳转:

将不带www的跳转到带www的:在根目录下新建.htaccess文件,写入:

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} ^manyi.cc [NC]

RewriteRule ^(.*)$ http://www.manyi.cc/$1 [L,R=301]

重定向到新域名:

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^(.*)$ http://www.manyi.cc/$1 [L,R=301]

3)在httpd.conf配置文件中配置:

<VirtualHost *:80>

ServerName manyi.cc

RedirectMatch permanent ^/(.*) http://www.manyi.cc/$1

</VirtualHost>

使用正则进行301伪静态配置:(将news.php?id=123这样的地址转向到news-123.html)

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^news-(.+)\.html$ news.php?id=$1

FastCGI加载PHP伪静态设置的注意事项

?

1

2

3

4

5

6

7

8

9

10

默认的"RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]"规则在apache fastcgi模式下会导致"No input file specified".

修改成

RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]

这样就好了,地址正常重写。

#php api模式,服务器能识别PATH_INFO

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

#php fastcgi模式 服务器不识别PATH_INFO

RewriteRule ^(.*)$ index.php [E=PATH_INFO:$1,QSA,PT,L]