欢迎光临
感谢一路有你

伪静态/重定向学习整理

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

伪静态只是改变了url的表现形式,实际上还是动态页面

伪静态有真实静态页面一样的SEO

我们普遍使用重定向来实现伪静态

重定向

http协议中的3XX(主要有302/303)

例:

1.修改apache配置文件
AllowOverride All

2.
.htaccess文件
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.html

 

mod_rewrite

apache的url操作模块

包含RewriteBase/RewriteRule/RewriteCond/RewriteMap

logLevel使用规范:

生产模式下不要大于2

httpd.config
logLevel alert rewrite:trace8
只能在conf中设置,不能再.htaccess中设置
日志保存在apache_error.log

模式匹配

支持perl格式的正则表达式rewirte的变量

多个flag用逗号隔开

R

强制外部重定向,后面可以加301或302跳转,不加默认302

301永久重定向(SEO评分分继承)

302临时重定向(获取新的评分)

RewriteRule ^(.*)\.html$ /$1.html [R=302]
RewriteRule ^(.*)\.html$ /$1.html [R]

c

l链接下一规则,跟下一条规则成为一个整体

加C后当地一条规则匹配的情况下才进入第二条规则,否则不进入

不加C,规则都能访问

RewriteRule ^(.*)\.html$ /$1.html [C]
RewriteRule ^(.*)\.html$ /$1.php
招的是  .PHP 文件

L

结尾规则,立即停止重写,并不再应用其他重写规则

RewriteRule ^(.*)first.php?req=$1 [L]
RewriteRule ^(.*)first11111.php?req=$1

NE

不对url中的特殊字符进行hexcode转码

RewriteRule ^(.*)\html /index.hml#$1 [R,NE]  //不加#会被转义

NC

不区分大小写

RewriteRule ^t/(.*)\html /index.hml#$1 [NC]  ///t的大小写

G

请求网站已经时效

Apache返回410

RewriteRule ^(.*)\oldurl.*$-[G]

QSA

用于url中截取查询字符串

RewriteRule ^per/(.*)$ /per.php?person_id=$1 [QSA,R]
/per/123.php?name=xxxx

RewriteBase设置目录

RewriteBase Url-path

设置目录级重写的基准url

RewriteBase /

RewriteRule ^(.*)\.shtml$ $1.html

RewriteCond

定义一个规则条件

RewriteCond $1 "test"
RewriteRule ^(.*)\.html $1.hml
先进行rule匹配,匹配成功,查找是否有cond ,如果有则判断是否符合,,,,符合rule生效,不符合rule不生效

服务器变量%

RewriteCond %{HTTP_HOST} "127.0.0.1"
RewriteRule ^(.*)\.html http://localhost/$1.hml [R]
当时用127.0.0.1访问时,自动跳转到localhost

%1-9

RewriteCond %{HTTP_HOST} "127.0.0.(.*)"
RewriteCond %1 "2"
RewriteRule ^(.*)\.html http://localhost/$1.hml [R]
满足第一个cond,还得满足第二个cond

RewritePattern

-d是否是目录

-f是否是文件

NC

OR

RewriteCond C:/wamp/www -d [OR]
RewriteCond C:/www1
RewriteRule ...............
只要一个满足即可满足

RewriteMap

MapName:命名给rewriteRule调用

MapType:map的文件类型有txt  rnd

MapSource:map文件目录(最好别放到用户能访问的路径)

放到整个配置项的外面

txt格式:

${MapName:LookupKey | DefaultValue}
1.找到httpd.conf  在配置项上面(<directory "C:/wamp.....">)前加入
RewriteMap pages txt:C:/wamp/map.txt
加入后重启
2.建立文件map.txt
test1 pagetest1
test2 pagetest2
3..htacess配置
RewriteRule ^(.*),shtml ${pages:$1|page}.php

rnd格式:

随机映射

RewriteMap servers rnd:C:/wamp/rnd.txt

URL1 S1|S2|S3
URL2 S4|S5

RewriteRule ^/(.*\.(png|gif|jpg))
http://${servers:URL1}/$1

临时重定向/永久重定向

302重定向(可以对搜索引擎作弊,尽量不用

防盗链

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.wangmingchang.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.wangmingchang.com/.*$ [NC]

RewriteRule .(gif|jpg|png|jpeg|bmp)$ https://www.xiaozhou.net/pics/404.jpg [R,NC]

https://xiaozhou.net/enable_rewrite_module_of_apache-2012-02-15.html

限制访问者IP列表

RewriteMap hosts-deny txt:c:/wamp/hosts.deny  //存储限制Ip
RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOTFOUND} deny [OR] 
RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOTFOUND} deny
RewriteRule ^ - [F]

 

http://www.phpcms.cn/html/2011/huanjingdajian_1024/373.html

限制迅雷

RewriteCond %{HTTP_USER_AGENT} 2.0.50727 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^BlackWido [NC,OR]
RewriteRule .abuse.txt

tp重定向

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

 

 

赞(0) 打赏
未经允许不得转载:王明昌博客 » 伪静态/重定向学习整理
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏