apache的rewrite机制
当我们使用thinkphp的时候,比如说我们访问一个Test控制器的test方法,http://localhost/index.php/Test/test/1.html,那个这个1是用get方式传递的参数,假如这个参数为id,那么我们其实访问的真实地址是http://localhost/index.php/Test/test?id=1.html,用过thinkphp的都知道,其实这个就是伪静态,通过配置tp配置文件的url模式可以实现,但是伪静态的实现,其实也可以用apache的rewrite机制实现
apache的rewrite机制的实现:
比如 http://www.test.com/test.php?type=music&id=100
希望上面的url 变成
http://www.test.com/test-music-id100.html
1.先启用rewrite模块
修改apache中conf目录下httpd.conf文件,找到LoadModule rewrite_module modules/mod_rewrite.so并打开
2.配置虚拟域名
(1)打开apache中conf目录下的vhosts.conf,并引入conf/extra/httpd-vhosts.conf,代码如下
Include conf/extra/httpd-vhosts.conf
(2)修改httpd-vhost.conf文件,代码如下
<VirtualHost *:80>
DocumentRoot "D:/phpstudy/www/test"
<Directory "D:/phpstudy/www/test">
</Directory>
</VirtualHost>
其中
DocumentRoot是网站根目录
Directory配置节点,用于指定该目录下的文件或是图片.的访问权限,也可以设置虚拟主机的错误页面,欢迎页面等
(3)在C:\Windows\System32\drivers\etc目录下的hosts文件,设置ip和主机的对应关系,代码如下
127.0.0.1 www.test.com
(4)访问127.0.0.1 www.test.com/test.php
配置成功!
3.配置rewrite机制
一般来说,httpd-vhost.conf文件是不能让我们程序员随便配置的,所以我们可以使用.htaccess文件进行配置
(1)修改httpd-vhost.conf文件,开启用.htaccess文件去匹配规则,代码如下
allowoverride all
(2)在网站根目录建立.htaccess文件,代码如下
<IfModule rewrite_module>
#rewrite 模块启用
RewriteEngine On
#配置我们的rewrite规则
RewriteRule test-(\w+)-id(\d+)\.html$ test.php\?type=$1\&id=$2
</IfModule>
其中rewriteRule使用正则表达式来匹配的,匹配的部分是域名后面的字符串,也可以同时配置多个规则(RewriteRule)
(3)重启apache并测试(注意,只是修改.htaccess是不需要重启apache的)
php代码
测试结果
关于Directory其实还有许多常用的配置的,因为这次主要是记录伪静态的实现,所以等下次有时间的话我会继续整理一份关于Directory的常用配置
apache的rewrite机制的更多相关文章
- 页面静态化3 --- 伪静态技术之Apache的rewrite机制
Apache的rewrite机制: 意思就是,你发送的地址,比如:http://localhost/news-id67.html会被Apache改写成http://localhost/news.p ...
- apache的rewrite机制配置
步骤: 1:启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_rewr ...
- apache配置rewrite及.htaccess文件(转载)
今天看到一个哥们的帖子发了个rewrite的帖子,以前也写过一个,配置挺简单的,但当时没注意这个问题,当时没有用到.htaccess文件,在机子上测试了一下,发现确实没法用,于是开始找问题的所在. 自 ...
- apache url rewrite 的RewriteRule参数详解
apache url rewrite 的RewriteRule参数详解 我们经常会在apache那边对一些url进行一些重写操作,那么就会涉及到很多的重写策略的选择,比如重定向的方式,参数的处理,规则 ...
- Magento - Rewrite机制一窥
看一个url例子 http://localhost/magento/index.php/customer/account/login 这里假定http://localhost/magento/ 是ma ...
- Apache的rewrite规则详细介绍
Apache的rewrite规则详细介绍 发布日期:2008-09-02 16:16 来源: 作者: 点击:7044 rewrite标志 R[=code](force redirect) 强制外部重定 ...
- Apache Lucene评分机制的内部工作原理
Apache Lucene评分机制的内部工作原理' 第5章
- 【转】CentOs中Apache开启rewrite模块详解
rewrite是apache环境的一个伪静态功能了,如果我们没有没让Apache开启rewrite功能,网站上所有的rewrite规则都不可使用. centos的配置文件放在: /etc/httpd/ ...
- Nginx与Apache的Rewrite规则的区别
一.Nginx Rewrite规则相关指令 Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx R ...
随机推荐
- 【Spark机器学习速成宝典】模型篇08支持向量机【SVM】(Python版)
目录 什么是支持向量机(SVM) 线性可分数据集的分类 线性可分数据集的分类(对偶形式) 线性近似可分数据集的分类 线性近似可分数据集的分类(对偶形式) 非线性数据集的分类 SMO算法 合页损失函数 ...
- Linux_用户和权限管理
目录 目录 用户管理基础知识 用户管理指令 useradd userdel passwd usermod chage su id who chmod chown s权限t权限 acl 用户管理基础知识 ...
- Centos下使用subversion
Centos下使用subversion 1.安装 yum install subversion 2.创建版本库 a.mkdir -p /var/svn/repositories(创建版本库目录) b. ...
- vue中html、css、js 分离
在正常的创建和引用vue文件都是html.css.js三者在一起的,这样写起来虽然方便了,但是页面比较大或者代码比较多的情况下,即使使用组件有时代码也比较多,简单来说查找不变不利于编程,大的来说影像优 ...
- HTTP Status 500 ? Internal Server Error
getWriter()和getOutputStream()不能同时调用 HTTP Status 500 ? Internal Server Error Type Exception Report Me ...
- nanomsg安装和测试
最近在构建一个中间层的通信架构,本来想用dbus,在实验过程中发现dbus对于国产系统支持版本比较低,安装比较麻烦,今天无意中看中了nanomsg,尽管没有dbus那么强悍的生态,但基本能满足需求. ...
- python基本成分详解
字符串 一.需要了解的 msg = 'HelloWorld' 1.isdecimal判断是否是十进制(整数)w print(msg.isdecimal()) 2.endswith判断字符串末尾是否是指 ...
- yum本地源和网络源的配置
一.yum本地源 1. 删除YUM库[root@tianyun ~]# rm -rf /etc/yum.repos.d/* 2.挂载安装光盘(临时):[root@tianyun ~]# m ...
- 手把手带你发布Nuget包-图文说话
博客:https://www.cnblogs.com/24klr/
- 百度之星 2019 预赛三 A 最短路 1
题目链接 分析 异或运算满足「三角不等式」. $\forall a, b, c \in \mathbb{Z}_{\ge 0}$,有 $a \xor b \le (a \xor c) + (c \xor ...