apache伪静态

简介: 打开apache的配置文件httpd.conf 找到 #LoadModule rewrite_module modules/mod_rewrite.so 把前面#去掉。没有则添加,但必选独占一行,使apache支持 mod_rewrite 模块 找到 <Directory "D:/ApacheS.
+关注继续查看

打开apache的配置文件httpd.conf

找到

#LoadModule rewrite_module modules/mod_rewrite.so

把前面#去掉。没有则添加,但必选独占一行,使apache支持 mod_rewrite 模块


找到

<Directory "D:/ApacheServer/web">

    #

    # 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


    #

    # Controls who can get stuff from this server.

    #

    Order allow,deny

    Allow from all


</Directory>

AllowOverride None 换成 AllowOverride All 使apache支持 .htaccess 文件


重启apache服务器


在要启用伪静态的 PHP 项目根目录下建立 .htaccess 文件


在 .htaccess 文件中输入内容


<IfModule mod_rewrite.c>

    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

</IfModule>


注释:

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-9.html

http://localhost/test/page-18.html


都会转向 http://localhost/test/index.php 页面

并且依次

http://localhost/test/index.html     页面什么都不显示

http://localhost/test/index-9.html  页面显示 p : 9

http://localhost/test/page-18.html   页面显示 action : pageid : 18


相关文章
|
自然语言处理 搜索推荐 Apache
手把手带你入门Apache伪静态的配置
网站伪静态,主要是为了增加搜索引擎的友好度,方便网站内容被搜索引擎收录而诞生的。类似网站上常用的301重定向、404页面的设置等在SEO方面是必不可少的。 伪静态是相对真实静态来讲的,通常我们为了增强搜索引擎的友好面,都将文章内容生成静态页面,但是有的朋友为了实时的显示一些信息。或者还想运用动态脚本解决一些问题。不能用静态的方式来展示网站内容。但是这就损失了对搜索引擎的友好面。
495 1
手把手带你入门Apache伪静态的配置
热门文章
最新文章
相关产品
云迁移中心
推荐文章
更多
推荐镜像
更多