最所学习 .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的目录设置一下即可。
希望对大家有所帮助!
分享到:
相关推荐
收集的学习资料,包括:Derby数据库(V10.9)用户手册(PDF版),用 Apache Derby 进行 Java 数据库开发,用 Apache Derby 进行数据库开发,03开源项目(三)嵌入式数据库Apache Derby(1)(开发指南).pdf ,Apache Derby ...
[Packt Publishing] Apache Karaf 学习教程 (英文版) [Packt Publishing] Learning Apache Karaf (E-Book) ☆ 图书概要:☆ Develop and deploy applications using the OSGi-based runtime container, Apache ...
配置APACHE服务器(linux-Apache)[整理].pdf
Apache配置[整理].pdf
apache mina的入门完整学习资料,附加中文参考手册。
apache学习资料!!
详细介绍Apache Spark的机器学习库,对学习Apache Spark初学者有很大帮助,并且对Spark相关机器学习库都有介绍!
apache的学习笔记
Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)
IO,Lang,Bean,Configuration,Codec,Collection
Apache cxf 学习笔记,供大家参考
APACHE BIRT图表学习笔记,APACHE BIRT图表学习笔记
Apache_JMeter基础学习PDF
Apache Pluto Portal 文档说明 简单易懂 适合初学者
NULL 博文链接:https://tangmingjie2009.iteye.com/blog/2036903
本项目是基于Scala语言开发的Apache Spark学习与实践源码,共包含53个文件。其中,Scala源代码文件21个,文本文件7个,XML文件5个,Markdown文档5个,JSON配置文件3个,Avro Schema文件2个,CSV数据文件2个,...
Apache cxf 学习笔记.pdfApache cxf 学习笔记.pdf
MINA-2.0.0-M4.chm(英文)中文的实在是没有)+Mina2.0学习笔记(修订版).doc+Apache_Mina_Server_2.0中文参考手册V1.0.pdf+MINA框架新人指南.pdf
Apache Felix Web Console Bundle整理包 管理控制台 框架运行环境 idea使用集成包 包含两个demo实例
Apache Nutch 1.7 学习总结,包含详细的Windows和Linux下的安装步骤,有图示。