centos-rhel服务器相关 / php / 未分类 · 2015年5月6日

关于Apache mod_rewrite php 伪静态 实现URL重写和防盗链功能

mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面。
1.检测Apache是否支持mod_rewrite
通过php提供的phpinfo()函数查看环境配置,通过Ctrl+F查找到“Loaded Modules”,其中列出了所有apache2handler已经开启的模块,如果里面包括“mod_rewrite”,则已经支持,不再需要继续设置。
如果没有开启“mod_rewrite”,则打开目录 您的apache安装目录“conf/” 下的 httpd.conf 文件,通过Ctrl+F查找到“LoadModule rewrite_module”,将前面的”#”号删除即可。
如果没有查找到,则到“LoadModule” 区域,在最后一行加入“LoadModule rewrite_module modules/mod_rewrite.so”(必选独占一行),然后重启apache服务器即可。
2.让apache服务器支持.htaccess
如何让自己的本地APACHE服务器支持”.htaccess”呢?其实只要简单修改一下apache的httpd.conf设置就可以让 APACHE支 持.htaccess了。打开httpd.conf文件(在那里? APACHE目录的CONF目录里面),用文本编辑器打开后,查找
Options FollowSymLinks
AllowOverride None
改为
Options FollowSymLinks
AllowOverride All
就可以了。
当然改的要是你自己想要的

Options FollowSymLinks
AllowOverride All

3.建立.htaccess 文件
如果是在windows平台下,刚开始还真不知道怎么建立”.htaccess”文件,因为这个文件实际上没有文件名,仅仅只有扩展名,通过普通方式是无法建立这个文件的,别着急,马上告诉你三种方法:三种方法都是先建立一个htaccess.txt的文本文件(当然,这个文本文件的名字你可以随便取),然后有三种方式给这个文件重命名:
(1)用记事本 打开,点击文件–另存为,在文件名窗口输入”.htaccess”,注意是整个绿色部分,也就是包含英文引号,然后点击保存就行了。
(2)进入cmd命令 窗口,通过cd切换当刚建立htaccess.txt文件的文件夹,然后输入命令:rename htaccess.txt .htaccess ,然后点击键盘Enter键即可。
(3)通过ftp连接htaccess.txt所在文件夹,通过ftp软件重命名。
4.rewrite规则学习
我们新建一个.htaccess文件之后,就在里面写入以下内容:
RewriteEngine on #rewriteengine为重写引擎开关on为开启off为关闭
RewriteRule ([0-9]{1,})$index.php?id=$1
我讲解一下RewriteRule:RewriteRule是重写规则,支持正则表达式的,上面的([0-9]{1,})是指由数字组成的,$是结束标志,说明是以数字结束!
好吧,现在我们可以实现伪静态页面了,写下一下的规则:

RewriteEngine on
RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$index.php?action=$1&id=$2

([a-zA-Z]{1,})-([0-9]{1,}).html$是规则,index.php?action=$1&id=$2是要替换的格式,$1代表第一个括号匹配的值,$2代表第二个,如此类推!!
我们写一个处理的PHP脚本:
index.php
PHP代码
’;
echo ‘你的ID是:’ . $_GET[‘id’];
?>
好了,我们现在在浏览器中输入:
localhost/view-12.html
输出的是:
你的Action是:view
你的ID是:12
#

#
# Possible values for the Options directive are “None”, “All”,
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that “MultiViews” must be named *explicitly* — “Options All”
# doesn’t give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
#AllowOverride None
AllowOverride All
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all

关于Apache mod_rewrite的中文配置、使用和语法介绍(实现URL重写和防盗链功能)
http://chinablog.blog.51cto.com/276793/280278
http://blog.csdn.net/zenwong/article/details/3093444
http://www.douban.com/note/98028752/
http://wenku.baidu.com/view/1f21bb651ed9ad51f01df21e.html