伪静态和重定向(apache)学习笔记,apache


日志

错误日志文件:

/logs/error.log(或error_log)

日志级别(级别从高到低):

debug, info, notice, warn, error, crit, alert, emerg.

低级别的日志选项会包括高级别的信息,如info会包含notice、warn的信息
默认配置:

LogLevel warn

调试配置(举例):

LogLevel alert rewrite:trace8

.htaccess语法

Flag使用

重定向(内部跳转301)

举例:将.htm访问跳转到.html上

RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.shtml

R 重定向(外部跳转302)

备注:R方式的重定向默认是相对一个基准路径的,加上/才会访问当前根目录下。可以通过设置RewriteBase来设置基准路径。
举例:将.htm访问跳转到根目录下的.html上

RewriteEngine on
RewriteRule ^(.*)\.htm$ /$1.html [R]

301和302的区别
301 永久重定向,SEO友好,网站SEO评价会传到重定向网址
302 临时重定向

C 满足条件执行下一跳规则

例子:*.htm会跳转到*.php,*.html不会跳转到*.php

RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.html [C]
RewriteRule ^(.*)\.html$ $1.php

L 该规则为最后一条执行规则,下面的规则都不执行

例子:

RewriteEngine on
RewriteRule ^(.*)\.html$ first.php?url=$1 [L]
RewriteRule ^(.*)\.html$ second.php?url=$1

NE 特殊字符不进行转义

例子:会显示#(没有转义)

RewriteEngine on
RewriteRule ^(.*)\.html$ /index.php#$1 [R,NE]

NC 匹配模式大小写不敏感

例子:/AAA/… 也会被匹配

RewriteEngine on
RewriteRule ^aaa/(.*)\.html$ /bbb/$1.html [NC]

G 链接失效(GONE)

例子:

RewriteEngine on
RewriteRule ^aaa/(.*)$ - [G]

QSA 截取URL中的键值对

例子:访问per/index.php?name=xiaoming会跳转到/per.php?url=index.php&name=xiaoming

RewriteEngine on
RewriteRule ^per/(.*)$ /per.php?url=$1 [R,QSA]

RewriteBase 配置跳转基准路径

例子:

  • 内部跳转:直接替换
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.html
  • 外部跳转:替换时会加上绝对路径,所以一般需要在$1前加上/来实现相对跳转功能
RewriteEngine on
RewriteRule ^(.*)\.htm$ /$1.html [R]
  • 若要用外部跳转实现内部跳转效果,需要配置基准跳转路径RewriteBase
RewriteEngine on
RewriteBase /
RewriteRule  ^(.*)\.htm$ $1.html [R]

内部跳转和外部跳转(重定向)的区别
内部跳转url不会改变
外部跳转url会改变成跳转到的网址

RewriteCond 条件控制

很容易理解,满足Rewritecond的,才会执行RewriteRule
举例:

  • 使用rule变量
    例子:URL中含有test的才能执行RewriteRule
RewriteEngine on
RewriteCond $1 "test"
RewriteRule ^(.*)\.htm$ $1.html
  • 使用系统变量
    例子:当访问host为127.0.0.1时,自动跳转到localhost
RewriteEngine on
RewriteCond %{HTTP_HOST} "127.0.0.1"
RewriteRule ^(.*)$ http://localhost/$1 [R]
  • 使用cond变量
    例子:先取出地址最后一位数字,存放在%1中,如果等于1,则执行rule
RewriteEngine on
RewriteCond %{HTTP_HOST} "127.0.0.(.*)"
RewriteCond %1 "2"
RewriteRule ^(.*)$ http://localhost/$1 [R]
  • 特殊标记
判断为目录 -d
判断为文件 -f

条件判断 [OR]

加[OR]之后,只要有cond之间一个满足,即可执行rule
而默认情况是所有cond之间为and,即所有cond成立,才执行rule
例子:下面例子中会执行rule

RewriteEngine on
RewriteCond C:/Windows/ -d [OR]
RewriteCond C:/Windows/ -f
RewriteRule ^(.*)\.htm$ $1.html

RewriteMap使用

apache的rewrite模块进行rewrite的时候,需要将规则直接写到http.conf的rewrite模块中,并且规则变动一次就需要重启apache服务器一次。这个让开发者比较郁闷。这里有一个rewriteMap解决了部分的问题,他的工作就是将一个映射关系以Map的形式存储在一个文件中,我们可以通过修改这个文件的对应关系而不需要重启apache服务器就可以应用映射关系。
常用格式
txt格式 一般映射
rnd格式 随机映射,匹配结果可能有多个
例子1:浏览器访问localhost/tt1会访问test11。。。
httpd.conf内部

RewriteMap pages txt:E:/page.txt

.htaccess中

RewriteRule ^(.*).html$ ${maps:$1|NotFound}

page.txt中

#首行加注释或空一行可以得出正常结果,原因待分析
tt1 test11
tt2 test22

例子2:浏览器访问localhost/URL1会随机访问localhost/S1(或S2、S3)
在httpd.conf内部

RewriteMap dirs rnd:E:/dir.txt

.htaccess中

RewriteRule ^(.*).html$ /$(dirs:$1|NotFound)/$1.php

dir.txt中

#首行加注释或空一行可以得出正常结果
URL1 S1|S2|S3
URL2 S4|S5

应用

防盗链

例子:图片只能通过localhost访问

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !localhost [NC]
RewriteRule \.(gif|png|jpg|jpeg)$ - [F,NC]

防盗链针对http_referer而制定,部分浏览器(或插件)做了一些改变,发送请求不带http_referer,这种方法则无法防盗链

限制IP访问(重定向)

httpd.conf中

#拒绝指定IP访问(重定向)
RewriteMap hosts-deny txt:E:/host-deny.txt

.htaccess中

RewriteEngine on
RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NotFound} deny 
RewriteRule ^ - [F]

host-deny中添加限制IP

#测试限制IP
192.168.2.100 deny
127.0.0.1 deny

相关内容

    暂无相关文章