Rewrite – Apache转Lighttpd的伪静态rewrite方法

市面上大部分伪静态都是Apache下的.htaccess,因为Apache的伪静态可以独立为文件,所以Apache比较适合IDC,所以也导致Lighttpd的伪静态比较少开源程序会提供出来。

其实Lighttpd的rewrite写法和Apache的大同小异,无非是正则嘛,本人认为最大的区别在于Lighttpd的rewrite规则是写在了lighttpd.conf中。

首先在lighttpd的配置文件中要加入rewrite模块:

server.modules = ("mod_rewrite") 

lighttpd伪静态放置位置为url.rewrite-once=() 中,格式为:

url.rewrite-once = ( 
"" => "", 
"" => ""
)

这个url.rewrite-once放置的位置就自己看着办了。要哪部分生效就放哪里。
再说一下Apache的.htaccess如何转换为lighttpd的
以下是Destoon系统的apache伪静态规则:

# Destoon B2B Rewrite Rules 
ErrorDocument 404 /404.php 
RewriteEngine On 
RewriteBase / 
RewriteRule ^(.*)\.(asp|aspx|asa|asax|dll|jsp|cgi|fcgi|pl)(.*)$ /404.php 
RewriteRule ^(.*)-htm-(.*)$ $1.php?$2 
RewriteRule ^(.*)/show-([0-9]+)([\-])?([0-9]+)?\.html$ $1/show.php?itemid=$2&page=$4 
RewriteRule ^(.*)/list-([0-9]+)([\-])?([0-9]+)?\.html$ $1/list.php?catid=$2&page=$4 
RewriteRule ^(.*)/show/([0-9]+)/([0-9]+)?([/])?$ $1/show.php?itemid=$2&page=$3 
RewriteRule ^(.*)/list/([0-9]+)/([0-9]+)?([/])?$ $1/list.php?catid=$2&page=$3 
RewriteRule ^(.*)/([a-z]+)/(.*)\.shtml$ $1/$2/index.php?rewrite=$3 
RewriteRule ^(com)/([a-z0-9_]+)/([a-z]+)/(.*)\.html$ index.php?homepage=$2&rewrite=$4 
RewriteRule ^(com)/([a-z0-9_]+)/([a-z]+)([/])?$ index.php?homepage=$2 
RewriteRule ^(com)/([a-z0-9_]+)([/])?$ index.php?homepage=$2

转成Lighttpd后:

url.rewrite-once=( 
"^(.*)\.(asp|aspx|asa|asax|dll|jsp|cgi|fcgi|pl)(.*)$" => "/404.php", 
"^(.*)-htm-(.*)$" => "$1.php?$2", 
"^(.*)/show-([0-9]+)([\-])?([0-9]+)?\.html$" => "$1/show.php?itemid=$2&page=$4", 
"^(.*)/list-([0-9]+)([\-])?([0-9]+)?\.html$" => "$1/list.php?catid=$2&page=$4", 
"^(.*)/show/([0-9]+)/([0-9]+)?([/])?$" => "$1/show.php?itemid=$2&page=$3", 
"^(.*)/list/([0-9]+)/([0-9]+)?([/])?$" => "$1/list.php?catid=$2&page=$3", 
"^(.*)/([a-z]+)/(.*)\.shtml$" => "$1/$2/index.php?rewrite=$3", 
"^(com)/([a-z0-9_]+)/([a-z]+)/(.*)\.html$" => "index.php?homepage=$2&rewrite=$4", 
"^(com)/([a-z0-9_]+)/([a-z]+)([/])?$" => "index.php?homepage=$2", 
"^(com)/([a-z0-9_]+)([/])?$" => "index.php?homepage=$2"
)

简单来说就几步(替换含双引号和空格,【】中为内容):

1、把无用的RewriteEngine On和RewriteBase /去掉,只剩下RewriteRule开头的内容;

2、把【RewriteRule 】(注意空格)替换为双引号【”】;

3、把apache规则里每条规则中间的空格替换为【” => “】;

4、在每条规则后加上【”,】,注意最后一条不要只加【”】原因自己理解;

5、基本完成,在整段内容前加上【url.rewrite-once=(】,整段内容后加上【)】;

6、完成。

点赞