---
title: "apche如何使用rewrite实现伪静态"
author: "ideaforj"
date: "2012-10-24T16:57:10Z"
tags: []
categories: ["编程语言"]
slug: "apche-rewrite"
draft: false
lastmod: "2012-10-24T16:57:10Z"
id: 1703295
url: "https://ideaforj.iteye.com/blog/1703295"
---

&lt;!-- \[if gte mso 9]&gt;&lt;xml&gt; &lt;w:WordDocument&gt; &lt;w:View&gt;Normal&lt;/w:View&gt; &lt;w:Zoom&gt;0&lt;/w:Zoom&gt; &lt;w:PunctuationKerning/&gt; &lt;w:DrawingGridVerticalSpacing&gt;7.8 磅&lt;/w:DrawingGridVerticalSpacing&gt; &lt;w:DisplayHorizontalDrawingGridEvery&gt;0&lt;/w:DisplayHorizontalDrawingGridEvery&gt; &lt;w:DisplayVerticalDrawingGridEvery&gt;2&lt;/w:DisplayVerticalDrawingGridEvery&gt; &lt;w:ValidateAgainstSchemas/&gt; &lt;w:SaveIfXMLInvalid&gt;false&lt;/w:SaveIfXMLInvalid&gt; &lt;w:IgnoreMixedContent&gt;false&lt;/w:IgnoreMixedContent&gt; &lt;w:AlwaysShowPlaceholderText&gt;false&lt;/w:AlwaysShowPlaceholderText&gt; &lt;w:Compatibility&gt; &lt;w:SpaceForUL/&gt; &lt;w:BalanceSingleByteDoubleByteWidth/&gt; &lt;w:DoNotLeaveBackslashAlone/&gt; &lt;w:ULTrailSpace/&gt; &lt;w:DoNotExpandShiftReturn/&gt; &lt;w:AdjustLineHeightInTable/&gt; &lt;w:BreakWrappedTables/&gt; &lt;w:SnapToGridInCell/&gt; &lt;w:WrapTextWithPunct/&gt; &lt;w:UseAsianBreakRules/&gt; &lt;w:DontGrowAutofit/&gt; &lt;w:UseFELayout/&gt; &lt;/w:Compatibility&gt; &lt;w:BrowserLevel&gt;MicrosoftInternetExplorer4&lt;/w:BrowserLevel&gt; &lt;/w:WordDocument&gt; &lt;/xml&gt;&lt;!\[endif]--&gt;

&lt;!-- \[if gte mso 9]&gt;&lt;xml&gt; &lt;w:LatentStyles DefLockedState="false" LatentStyleCount="156"&gt; &lt;/w:LatentStyles&gt; &lt;/xml&gt;&lt;!\[endif]--&gt;&lt;!-- \[if gte mso 10]&gt; &lt;style&gt; /* Style Definitions \*/ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} &lt;/style&gt; &lt;!\[endif]--&gt;

1.       配置 apache, 启用 rewrite 机制

在 httpd.conf 中启用 rewrite

LoadModule rewrite\_module modules/mod\_rewrite.so

2.       配置虚拟主机

到 c 盘 hosts 文件添加你的 ip 和 域名对应

127.0.0.1       [www.hsp.com](http://www.hsp.com/)

在 http.conf 文件中启用虚拟主机

\# Virtual hosts 启用虚拟主机

Include conf/extra/httpd-vhosts.conf

 

3\. 配置 httpd\_vhosts.conf 文件

 

 

 

 

在 &lt;Direcotry &gt; 段的配置，默认值，是在 httpd.conf 的中配置，看根目录 .

 

 

&lt;VirtualHost \*:80&gt;

    #ServerAdmin webmaster@dummy-host2.somenet.com

    # 文档的根目录

    DocumentRoot "C:/myenv/apache/htdocs/static3"

    # 域名

    ServerName www.hsp.com

    ErrorLog "logs/dummy-host2.somenet.com-error.log"

    #CustomLog "logs/dummy-host2.somenet.com-access.log" common

    # 配置 rewrite 相关选项 ,

    &lt;Directory "C:/myenv/apache/htdocs/static3"&gt;

    # 拒绝所有的访问

    #Deny from all

    Allow from all

    # 是否显示列表 ( 在发布项目后一般是不启用 , 对于这个配置，针对 DocumentRoot 在 htdos 外的目录生效 )

    #Options +indexes

    # 是否启用 rewrite

    #Allowoverride  all

    &lt;/Directory&gt;

&lt;/VirtualHost&gt;

4\. 在目录下添加了一个 .htaccess  文件，这个文件时配置 rewrite 机制和规则

 

&lt;IfModule rewrite\_module&gt;

\# 写你的 rewrite 规则

RewriteEngine On

#news-id(\\d+)\\.html$ 是规则   news.php?id=$1 是转发的页面

\# 正则 子表达式 捕获 反向引用

#  "news-id33.html"

\# 可以配置多个规则，匹配的顺序是从上到下

RewriteRule  news-id(\\d+)\\.html$   news.php?id=$1

RewriteRule  news-id(\\d+)\\.html$     error.php

&lt;/IfModule&gt;

 

 

综合使用案例

 

 

 

1.       这个 rewrite 的规则也直接在 &lt; Direcotry &gt; 段直接配置

 

 

&lt;Directory "C:/myenv/apache/htdocs/static3"&gt;

    # 拒绝所有的访问

    #Deny from all

     Allow from all

    # 是否显示列表 ( 在发布项目后一般是不启用 , 对于这个配置，针对 DocumentRoot 在 htdos 外的目录生效 )

    #Options +indexes

    # 是否启用 rewrite

    Allowoverride  all

    # 我们有时候，也可以把 rewrite 机制和规则写这里

    #RewriteEngine On

    #RewriteRule  news-id(\\d+)\\.html$   news.php?id=$1

    &lt;/Directory&gt;

 

 

使用伪静态来完成简单的新闻查询 .

☞ 伪静态是站在 seo 角度看 ., 他对于的页面不是存在的，而是每次查询数据库得到的信息 ,

真静态的页面是真的存在 .