欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

apache htccess 学习整理

程序员文章站 2022-07-14 17:14:58
...
最所学习  .htaccess  用法, 整理一些:
http://blog.csdn.net/bjbs_270/article/details/2097064
http://www.skygq.com/2011/02/21/apache%E4%B8%ADrewritecond%E8%A7%84%E5%88%99%E5%8F%82%E6%95%B0%E4%BB%8B%E7%BB%8D%E8%BD%AC/
http://www.3code.cn/rewriterule-htaccess%E8%AF%A6%E7%BB%86%E8%AF%AD%E6%B3%95%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/

简单罗列如下:

.换行符以外的所有字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,}重复n次或更多次
{n,m} 重复n到m次


应用替换时,前面第一个()中匹配的内容后面就用$1引用,第二个()中匹配的就用$2应用……


分析一下 discuz搜索引擎优化 htaccess 里面的重写。


RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2


首先加入用户通过 linuxidc.com/forum-2-3.html 访问discuz论坛,那么先通过.htaccess过滤,看看是否需要.htaccess引导一下用户,如果满足列出的一系列RewriteCond的 条件那么就进行重写,


discuz的没有列出RewriteCond 所以应该全部都进行重写。所以开始进行转写,forum-2-3.html 这个正好符合 列出的^forum-([0-9]+)-([0-9]+)\.html$ 正则表达式。并且 $1 为 2 ,$2为3 ,所以代


入后面,即 forumdisplay.php?fid=2&page=3 加上前面的RewriteBase 指定的文件目录,那么就带他到制定目录的forumdisplay.php?fid=2&page=3 。


二、常见的.htaccess应用举例(部分例子引自四个例子实战讲解.htaccess文件rewrite规则)


4.1 防止盗链,如果来得要访问jpe jpg bmp png结尾的url 用户不是来自我们的网站,那么让他看一张我们网站的展示图片。
RewriteEngine OnRewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]RewriteCond %{HTTP_REFERER} !^$RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]


4.2 网站升级的时候,只有特定IP才能访问,其他的用户将看到一个升级页面
RewriteEngine onRewriteCond %{REQUEST_URI} !/upgrade.html$RewriteCond %{REMOTE_HOST} !^24\.121\.202\.30


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


4.3把老的域名转向新域名
# redirect from old domain to new domainRewriteEngine OnRewriteRule ^(.*)$http://www.yourdomain.com/$1[R=301,L]


三、常用示例


RewriteEngine On
RewriteRule index.html index.php


比如:http://www.3code.cn/index.html  -> http://www.3code.cn/index.php


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


比如:http://www.3code.cn/test8.html  -> http://www.3code.cn/test.php?id=8


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


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


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


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


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


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


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


比如:http://www.3code.cn/cat5/ -> http://www.3code.cn/cat.php?id1=5


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


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

另:

解决在虚拟主机中使用Zend Framework时,根目录不能指定的问题

http://blog.chinaunix.net/uid-20787846-id-3058245.html


RewriteEngine on
RewriteRule ^(images/.+) /html/$1 [L] #修正图片请求,根据自己的目录来。
RewriteRule ^(css/.+) /html/$1 [L]
RewriteRule ^(jscript/.+) /html/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /html/index.php #其它找不到存在文件的请求全部转到html目录下的入口index.php
—————————-代表月亮消灭你分割线———————————–
这样问题就解决了,但又有一个新问题就是application和library目录下的文件可能会被用户访问到(直接url干进去),所以这个.htaccess文件可以再修改一下,针对deny的目录设置一下即可。



希望对大家有所帮助!