经过配置.htaccess实现伪静态

经过配置.htaccess实现伪静态

通过配置.htaccess实现伪静态

首先让Apache支持.htaccess,如果已经开启则跳过这一步

(用phpinfo()函数,搜索“mod_rewrite”,若查找到则已开启)。

 

1.配置文件httpd.conf

 

(1) 

Options Indexes FollowSymLinks

AllowOverride None 

 

改为 

Options Indexes FollowSymLinks

AllowOverride All 

 

(2)去掉下面的#号注释 

LoadModule rewrite_module modules/mod_rewrite.so

 

2.配置文件根目录下文件.htaccess

 

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

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

</IfModule>

 

windows 环境

RewriteRule  ^/abc$ /index.PHP  (httpd.conf 中有效,.htaccess 中无效)

RewriteRule  ^abc$  /index.php  (httpd.conf 中无效,.htaccess 中有效)

 

例:RewriteRule ([a-zA-Z0-9]{1,})-([a-zA-Z0-9]{1,}).html$ a/index.php?action=$1&id=$2

www.XXX.com/article-233.html

等价于www.XXX.com/a/index.php?action=article&id=233

 

例:RewriteRule a/([a-zA-Z0-9]{1,})$ a/index.php?id=$1

www.XXX.com/a/233

等价于www.XXX.com/a/index.php?id=233