apache php 开启伪静态
第一步 设置apache,让apache支持伪静态
1 打开Apache的配置文件httpd.conf
#LoadModule rewrite_module modules/mod_rewrite.so
2 把前面的 “#”去掉
#
# 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 None 改为 AllowOverride All
3让Apache支持.htaccess
Options FollowSymLinks
AllowOverride None
修改为
Options FollowSymLinks
AllowOverride All
重启Apache就可以生效
通过php提供的phpinfo()函数查看环境配置,通过Ctrl+F查找到“Loaded Modules”,其中列出了所有apache2handler已经开启的板块,假如里面包括“mod_rewrite”,则已经支持,不再需要继续设置。
第二部在项目根目录下创立.htaccess文件
在要启用伪静态的 PHP 项目根目录下建立 .htaccess 文件在 .htaccess 文件中输入内容
RewriteEngine on
RewriteRule index.html$ index.php
RewriteRule index-([1-9]+[0-9]*).html$ index.php?p=$1
RewriteRule ([a-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2
注释:RewriteEngine 为重写引擎开关,on为开启,off为关闭。RewriteRule 是路由转向规则,$ 之前路径为浏览器中要输入路径,这里可以用正则表达式表达。$+空格 后路径为后端实际转向路径,
转向后端实际路径时可以传参数,例子里的后端页面可以用$_GET['p'] $_GET['action'] $_GET['id'] 来接收
$1 代表浏览器路径中输入的第一个正则表达式的值,以此类推,$2代表第二个正则表达式的值
RewriteRule 路由转向规则里正则表达式用括号 () 括起来例子所在项目为test在项目下 index.php 页面内写入内容
<?php
if ($_GET ['p']) { echo “p : ” . $_GET ['p'];}
if ($_GET ['action']) { echo “action : ” . $_GET ['action'];}
if ($_GET ['id']) { echo “id : ” . $_GET ['id'];}
?>
在浏览器中输入
http://localhost/test/index.html
http://localhost/test/index-99.html
http://localhost/test/page-18.html
都会转向 http://localhost/test/index.php 页面
并且依次
http://localhost/test/index.html 页面什么都不显示
http://localhost/test/index-99.html 页面显示 p : 99
http://localhost/test/page-18.html 页面显示 action : pageid : 18
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » apache php 开启伪静态