Home  >  Article  >  php教程  >  Apache:如何利用.htaccess文件对PHP网站或文件进行伪静态处理,

Apache:如何利用.htaccess文件对PHP网站或文件进行伪静态处理,

WBOY
WBOYOriginal
2016-06-13 09:22:30762browse

Apache:如何利用.htaccess文件对PHP网站或文件进行伪静态处理,

来源:http://www.ido321.com/1123.html

今天get了一招:利用.htaccess文件对PHP网站或文件进行伪静态处理。

一、检查服务器是否支持伪静态处理:

必 须要空间支持 Rewrite 以及对站点目录中有 .htaccess 的文件解析,才有效.找到apache安装目录下的httpd.cof文件,去掉LoadModule rewrite_module modules/mod_rewrite.so前面的#(大概在154行,我的默认是开启)

二、在httpd.cof中查找以下部分:

<span><Directory />
   Options FollowSymLinks
   AllowOverride None
</Directory></span>

 

改为:

<span><Directory />
   Options FollowSymLinks
   AllowOverride All
</Directory></span>

 

如果没有,手动添加。(PS:LZ试过,在最近的新版本的Apache上即使没有这个,.htaccess照样生效。但是不同服务器及版本的原因,建议加上

三、重启apache服务器,添加.htaccess文件

建立.htaccess文件的方式:

1、保存文件的时候将文档保存成名为“***.txt”,再将其上传到服务器上,之后直接使用FTP软件来重命名为“.htaccess”。(适合windows)

2、保存文件的时候选择文件—>另存为,保存类型选所有文件,然后文件名输入.htaccess 。(适合windows)

3、直接在你的Unix或Linux虚拟主机上建立。

4、先用记事本编写好,随便保存为一个什么文件名,然后打开“命令提示符”(CMD),然后再用重命名命令(rename),例如:rename c:\htaccess.txt  .htaccess

请注意: .htaccess必须,保存为ANSI 格式,以ASCII模式上传,最好将其权限设置为644。

     一般我们将.htaccess文件放置在网站的根目录,控制所在目录及所有子目录,当然也可以放在网站的任何一个子目录下,但如果放置在子目录中,子目录中的指令会覆盖更高级目录或者主服务器配置文件中的指令。

在博客收录集(http://www.ido321.com/1112.html)为例,本地.htaccess文件如下:

<span>#rewriteengine为重写引擎开关on为开启off为关闭</span><span>
RewriteEngine On 
RewriteRule ^index\.html$ index.php
RewriteRule ^webmore\.html$ webmore.php</span>
   以index.html代替index.php    webmore.html代替webmore.php。    .htaccess支持正则表达式,例如:

  原始 news/detail.php?id=2  伪静态  news/detail_2.html

RewriteRule ^news/detail_([0-9]{1,})\.html$ news/detail.php?id=$1

 

下一篇:杂谈:HTML 5页面可视性API

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn