htaccess 伪静态规则

htaccess 伪静态规则

游戏|数码彩彩2024-03-31 7:40:10303A+A-

htaccess伪静态规则

.htaccess文件提供了针对目录改变配置的方法, 是Apache服务器中的一个配置文件,它负责相关目录下的网页配置。通过htaccess文件,可以帮我们实现:网页301重定向、自定义404错误页面、改变文件扩展名、允许/阻止特定的用户或者目录的访问、禁止目录列表、配置默认文档等功能。

伪静态实际上是利用php把当前地址解析成另外一种方法进行访问网站!要学伪静态规则的写法,你必须得懂一点正则。

一、正则表达式教程

简单罗列如下:

. 换行符以外的所有字符

w 匹配字母或数字或下划线或汉字

s 匹配任意的空白符

d 匹配数字

b 匹配单词的开始或结束

^ 匹配字符串的开始

$ 匹配字符串的结束

* 重复零次或更多次

+ 重复一次或更多次

? 重复零次或一次

{n} 重复n次

{n,}重复n次或更多次

{n,m} 重复n到m次

二、常见的.htaccess应用举例

1 防止盗链,如果来得要访问jpe jpg bmp png结尾的url 用户不是来自我们的网站,那么让他看一张我们网站的展示图片。

RewriteEngine On

RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]RewriteCond %{HTTP_REFERER} !^$RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

2 网站升级的时候,只有特定IP才能访问,其他的用户将看到一个升级页面

RewriteEngine on

RewriteCond %{REQUEST_URI} !/upgrade.html$RewriteCond %{REMOTE_HOST} !^24.121.202.30

RewriteRule $ http://www.xxx.com/upgrade.html [R=302,L]

3 把老的域名转向新域名

# redirect from old domain to new domain

RewriteEngine On

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

三、常用示例

比如:http://www.xxx.com/index.html -> http://www.xxx.com/index.php

RewriteEngine On

RewriteRule index.html index.php

比如:http://www.xxx.com/test8.html -> http://www.xxx.com/test.php?id=8

RewriteRule ^test([0-9]*).html$ test.php?id=$1

比如:http://www.xxx.com/cat-1-3.html -> http://www.xxx.com/cat.php?id1=1&id2=3

RewriteRule ^cat-([0-9]+)-([0-9]+).html$ cat.php?id1=$1&id2=$2

比如:http://www.xxx.com/cat-zbc2ac-3-5.html -> http://www.xxx.com/cat.php?id0=zbc2ac&id1=3&id2=5

RewriteRule ^cat-([a-zA-Z0-9-]*)-([0-9]+)-([0-9]+).html$ cat.php?id0=$1&id1=$2&id2=$3

比如:http://www.xxx.com/cat1-4-3-8.html -> http://www.xxx.com/cat1.php?id1=4&id2=3&id3=8

RewriteRule ^cat1-([0-9]+)-([0-9]+)-([0-9]+).html$ cat1.php?id1=$1&id2=$2&id3=$3

比如:http://www.xxx.com/cat5/ -> http://www.xxx.com/cat.php?id1=5

RewriteRule ^cat([0-9]*)/$ cat.php?id1=$1

比如:http://www.xxx.com/catm6/3/ -> http://www.xxx.com/catm.php?id1=6&id2=3

RewriteRule ^catm([0-9]*)/([0-9]*)/$ catm.php?id1=$1&id2=$2

点击这里复制本文地址 版权声明:本文内容由网友提供,该文观点仅代表作者本人。本站(https://www.angyang.net.cn)仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

昂扬百科 © All Rights Reserved.  渝ICP备2023000803号-3网赚杂谈