将htaccess转换为nginx

时间:2011-04-30 08:06:31

标签: .htaccess nginx rewrite

我没有运气将htaccess规则转换为nginx重写。我已经检查了NginxRewriteModule文档,并完成了一些,但更复杂的是我不知所措。以下是我正在看的内容:

RewriteRule    ^$                                               /cgi-bin/index.cgi [L]
RewriteRule    ([0-9A-Za-z]{12})-del-([0-9A-Za-z]+)/.+$         /cgi-bin/index.cgi?del=$1-$2 [L]

RewriteCond     %{REQUEST_FILENAME} !-f

RewriteRule    ^([0-9A-Za-z]{12})(\.html?|$)$                   /cgi-bin/index.cgi?op=download1&id=$1 [L]
RewriteRule    ^([0-9A-Za-z]{12})(\/.+|\.html?|$)               /cgi-bin/index.cgi?op=download1&id=$1&fname=$2 [L]
RewriteRule    ^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})$   /cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2 [L]
RewriteRule    ^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})(\/.+|\.html?|$)        /cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2&fname=$3 [L]

#RewriteRule    ^Reseller\.html$                                         /cgi-bin/Templates/Pages/english/Reseller.html [L]
RewriteRule    ^checkfiles\.html$                                       /cgi-bin/index.cgi?op=checkfiles [L]
RewriteRule    ^contact\.html$                                          /cgi-bin/index.cgi?op=contact [L]
RewriteRule    ^premium\.html$                                          /cgi-bin/index.cgi?op=payments [L]
RewriteRule    ^login\.html$                                            /cgi-bin/index.cgi?op=login [L]
RewriteRule    ^catalogue(.*)\.html$                                    /cgi-bin/index.cgi?op=catalogue&date=$1 [L]
RewriteRule    ^news([0-9]*)\.html$                                     /cgi-bin/index.cgi?op=news&page=$1 [L]
RewriteRule    ^n([0-9]+)-.*\.html$                                     /cgi-bin/index.cgi?op=news_details&news_id=$1 [L]
RewriteRule    ^free([0-9]+)\.html$                     /cgi-bin/index.cgi?op=registration&aff_id=$1 [L]
RewriteRule    ^users/([0-9A-Za-z\-_]{4,64})/?([0-9]+|$)        /cgi-bin/index.cgi?op=user_public&usr_login=$1&fld_id=$2 [L,NC]
RewriteRule    ^embedmp3-([0-9A-Za-z]{12})\.html$   /cgi-bin/index.cgi?op=mp3_embed&file_code=$1 [L]
RewriteRule    ^embedmp4-([0-9A-Za-z]{12})\.html$   /cgi-bin/index.cgi?op=mp32_embed&file_code=$1 [L]
RewriteRule    ^box$                                    /cgi-bin/index_box.cgi [L]

RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule    ^([0-9A-Za-z\-_]{4,64})(/[^\/]*/?|$)$            /cgi-bin/index.cgi?op=user_public&usr_login=$1&fld=$2 [L,NC]

RewriteCond     %{REQUEST_FILENAME} !-f
RewriteRule    ^([a-z0-9\-\_]+).html(.*)                        /cgi-bin/index.cgi?op=page&tmpl=$1$2 [L]

5 个答案:

答案 0 :(得分:36)

将Apache .htaccess转换为Nginx重写工具的在线工具包括:

请注意,这些工具将使用if语句转换为等效的rewrite expressions,但应将其转换为try_files。参见:

答案 1 :(得分:10)

尚未测试过,但看起来比the one Alex提到的要好。

winginx.com/en/htaccess上的说明:

  

关于htaccess to nginx converter

     

该服务是将Apache的.htaccess转换为nginx配置指令。

     

首先,该服务被认为是nginx转换器的mod_rewrite。但是,它允许您转换一些其他有理由从Apache移植到nginx的指令。

     

注意服务器指令(例如php_value等)将被忽略。

     

转换器不检查语法,包括正则表达式和逻辑错误。

     

请在使用前手动检查结果。

答案 2 :(得分:9)

重写规则与nginx的编写方式基本相同: http://wiki.nginx.org/HttpRewriteModule#rewrite

哪些规则会给您带来麻烦?我可以帮你翻译那些!

答案 3 :(得分:3)

使用此: http://winginx.com/htaccess

在线转换器,美妙的方式和节省时间;)

答案 4 :(得分:2)

您可以轻松制作一个Php脚本来解析旧的htaccess,我将这个用于PRestashop规则:

$ content = $ _POST ['content'];

    $lines   = explode(PHP_EOL, $content);
    $results = '';

    foreach($lines as $line)
    {
        $items = explode(' ', $line);

        $q = str_replace("^", "^/", $items[1]);

        if (substr($q, strlen($q) - 1) !== '$') $q .= '$';

        $buffer = 'rewrite "'.$q.'" "'.$items[2].'" last;';

        $results .= $buffer.PHP_EOL;
    }

    die($results);