一、Rewrite  

1、Rewrite规则简介

       Rewrite主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言,可基于服务级的(http.conf)和目录级的(.htaccess)两种方式。如果要想用到rewrite模块,必须先安装或加载rewrite模块。

安装Rewrite模块的两种方式:

方法一:是编译apache的时候就直接安装Rewrite模块

方法二:编译apache时以DSO模式安装apache,然后再利用源码和apxs来安装rewrite模块

针对该两种方式的具体介绍在之前的博文中具有详细的描述;

 

2、基于服务器级别的(httpd.conf)有两种方法:

方法一:在httpd.conf的全局下,直接利用RewriteEngine on来打开Rewrite功能;

方法二:在局部里利用RewriteEngine on来打开Rewrite功能,下面将会举例说明,需要注意的是,必须在每个virtualhost里用RewriteEngine on 来打开Rewrite功能,否则virtualhost里没有RewriteEngine on它里面的规则也不会生效;

      基于目录级的(./htaccess),要注意一点那就是必要打开此目录的FollowSymLinks属性且在./htaccess里要声明RewriteEngine on

 

3、Apache mod_rewrite规则重写的标志一览:

1) R[=code](force redirect)                 强制外部重定向
强制在替代字符串加上 http://thishost [:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。
2) F(force URL to be forbidden)            禁用URL,返回403HTTP状态码。
3) G(force URL to be gone)                  强制URL为GONE,返回410HTTP状态码。
4) P(force proxy)                                   强制使用代理转发。
***5) L(last rule)                               表明当前规则是最后一条规则,停止分析以后规则的重写。  -- 类似防火墙 - 一旦匹配则不继续
6) N(next round)        重新从第一条规则开始运行重写过程。
7) C(chained with next rule)          与下一条规则关联
如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。
8) T=MIME-type(force MIME type)            强制MIME类型
9) NS (used only if no internal sub-request)          只用于不是内部子请求
***10) NC(no case)             不区分大小写
11) QSA(query string append)             追加请求字符串
12) NE(no URI escaping of output)              不在输出转义特殊字符
例如:RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zed
13) PT(pass through to next handler)         传递给下一个处理
例如:
   RewriteRule ^/abc(.*) /def$1 [PT]             #   将会交给/def规则处理
   Alias /def /ghi
14) S=num(skip next rule(s))        跳过num条规则
15) E=VAR:VAL(set environment variable)        设置环境变量

 

4、mod_rewrite模块检查及安装

####检查模块是否安装、加载:

[[email protected] extra]# /usr/local/apache/bin/httpd -M | grep rewrite     #没有加载rewrite模块
[[email protected] extra]# ls /usr/local/apache/modules/* | grep rewrite    #在apache的安装目录中存在rewrite模块
/usr/local/apache/modules/mod_rewrite.so
[[email protected] extra]#
 

####在配置文件中加载rewrite模块

在配置文件中将下列行的注释去掉:

#LoadModule rewrite_module modules/mod_rewrite.so

改为

LoadModule rewrite_module modules/mod_rewrite.so

 

####再次查看已经加载上了:

[[email protected] extra]# /usr/local/apache/bin/httpd -M | grep rewrite
 rewrite_module (shared)
[[email protected] extra]# 

 

####重新启动Apache,让rewrite功能开启:

[[email protected] extra]# /etc/init.d/apachectl restart
 

5、Rewrite的使用实战

需求描述: 

使用www.zhanggeng.com 和 192.168.3.205可以访问服务页面;当Client使用如下网址时,可以跳转到www.zhanggeng.com

bbs.zhanggeng.com

www.zhanggeng.cn

www.zhanggeng.com.cn

空字符.zhanggeng.com

 

1)、在全局开启rewrite功能,在配置文件中添加如下内容;

Apache URL-Rewrite重写

2)、修改hosts解析文件,用于测试;

Apache URL-Rewrite重写

 

3)、重启Aapche

[[email protected] ~]# /etc/init.d/apachectl restart

 

4)、进行域名测试

###安装浏览器

[[email protected] ~]# yum install -y gdm firefox      #gdm是插件

 

####使用Xmanager进行连接

Apache URL-Rewrite重写

 

####然后就可以连接到Centos中的火狐浏览器进行测试:

Apache URL-Rewrite重写

 

####访问www.zhanggeng.com

Apache URL-Rewrite重写

 

####访问bbs.zhanggeng.com

Apache URL-Rewrite重写

 

####访问www.zhanggeng.com.cn

Apache URL-Rewrite重写

 

####访问zhanggeng.com

Apache URL-Rewrite重写

 

二、禁止目录浏览

       由于开启目录浏览会让我们整个目录下的内容全部都暴露到外面,因此我们必须要禁止目录浏览功能,当然一些目录开放给客户做下载的,可以忽略此项优化。

       我们通过修改apache主配置文件httpd.conf中的<Directory> </Directory> 标签内的Options选项参数来实现禁用目录浏览

 

####找一个文件多的目录将其放到htdocs下,用来测试

[[email protected] ~]# cp -a /boot/grub2/ /usr/local/apache/htdocs/
[[email protected] ~]# chown -R apache:apache /usr/local/apache/htdocs
 

####访问192.168.3.205/grub2/    会出现很多目录,是很不安全的

Apache URL-Rewrite重写

 

####将配置文件中目录下面的Option参数修改,来限制网站显示目录;

Apache URL-Rewrite重写

 

如果是 Apache2.2   则使用Option Indexes FollowSymLinks

如果是 Apache2.4   则使用Option FollowSymLinks

目前我的Apache环境是2.4的,所以修改后如下:

 Apache URL-Rewrite重写

 

####此时再去访问192.168.3.205/grub2/  就会变成Forbidden;如下

Apache URL-Rewrite重写

 

 


 

 

 

 

 

 

 

 

 

 

 

相关文章:

  • 2021-11-25
  • 2022-01-22
  • 2021-06-12
  • 2022-02-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-09
相关资源
相似解决方案