如何让的本地Apache服务器支持.htaccess?
如何使用.htaccess达到我们想要的效果?
开启Apache服务器支持
为了使Apache服务器支持.htaccess,需要修改httpd.conf文件,主要修改下面两个地方:
(1)找到
< Directory />
Options FollowSymLinks
AllowOverride None修改为
< Directory />
Options FollowSymLinks
AllowOverride All(2)去掉下面的注释
# LoadModule rewrite_module modules/mod_rewrite.so
.htaccess 写法
通常我们的url可能是http://localhost/index.php/post/index,有时为了简化url,我们希望能够省略index.php,直接写成http://localhost/post/index。下面就说说如何使用 .htaccess 达到这样的效果?
.htaccess 使用正则表达式进行条件匹配
< IfModule mod_rewrite.c>
# 将 RewriteEngine 模式打开
RewriteEngine on
# 如果商城程序存放于/shop下,需将下行更改为 RewriteBase /shop
RewriteBase /
# 匹配条件,(如果是目录和文件就直接访问,不再匹配下面的RewriteRule)
# %{REQUEST_FILENAME} 请求的文件名
# !-d 不是目录或不存在
# !-f 不是文件或不存在
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# apache,iis 伪静态规则
# [L] 这是最后一个匹配项,不再往下匹配
RewriteRule . index.php [L]
RewriteRule ^goods-([0-9]+)(-?)([0-9]*).html$ index.php?act=goods&goods_id=$1
# nginx 伪静态规则
#rewrite ^/goods-([0-9]+)(-?)([0-9]*).html$ /index.php?act=goods&goods_id=$1&id=$2 last;
< /IfModule>我们在Apache服务器中运行的话,只需要在程序根目录添加 .htaccess 文件,并写入下面的内容
< IfModule mod_rewrite.c>
RewriteEngine onRewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php
< /IfModule>保存后,使用简写url访问页面,是不是一样访问成功啦?

