将htaccess转换为nginx [关闭]

问题描述 投票:24回答:5

我没有运气将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]
.htaccess nginx rewrite
5个回答
36
投票

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

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


10
投票

尚未测试,但是外观比the one Alex提到的要好。

winginx.com/en/htaccess上的描述说:

关于htaccess to nginx converter

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

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

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

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

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


9
投票

重写规则几乎与nginx编写相同:http://wiki.nginx.org/HttpRewriteModule#rewrite

哪个规则给您带来麻烦?我可以帮助您翻译这些!


3
投票

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

在线转换器,节省时间和方法;)


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);
© www.soinside.com 2019 - 2024. All rights reserved.