使用 Apache 中 .htaccess 文件实现伪静态的部分实例分享

在网站建设中利用 .htaccess 实现伪静态功能在 Apache 的 web 开发中应该是经常用到的,下面汇总结了一些比较常用的利用 .htaccess 实现伪静态的例子与大家分享,很多都可以举一反三的用在自己网站的实际操作中。

 

1>将 index.php 伪静态成为 index.html

  1. RewriteRule ^index\.html$ index.php

 

2>将 news/info.php?id=3 伪静态成为 news/info_3.html

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

 

3>将 index.php?class_id=2&id=3 伪静态成为 2-3.html

  1. RewriteRule ([0-9]{1,})-([0-9]{1,})\.html$ index.php?class_id=$1&id=$2

([0-9]{1,})-([0-9]{1,})\.html$是规则,index.php?action=$1&id=$2是要替换的 url格式,$1代表第一个括号匹配的值,$2代表第二个,如此类推。

 

4>将 tag.php?tag=php教程 或者 tag.php?tag=程序员 伪静态成为 tag/php教程 和 tag/程序员

  1. RewriteRule ^tag/(.*)$ tag.php?tag=$1

 

5>设置自己网站的 404 跳转

  1. ErrorDocument 404 http://www.jianlove.com/404.html

 

6>禁止直接访问web目录中的后缀名为 .bak .inc 的文件

  1. <FilesMatch "\.(bak|inc)$">
  2. order deny,allow
  3. deny from all
  4. </FilesMatch>

 

未完,待续...

 

转载请注明链接地址:荐爱小站 » 使用 Apache 中 .htaccess 文件实现伪静态的部分实例分享

赞 (0) 赏 !

觉得文章有用就打赏一下吧,赠人玫瑰手有余香!

支付宝扫一扫打赏

微信扫一扫打赏