【问题标题】:convert htaccess to nginx [closed]将 htaccess 转换为 nginx [关闭]
【发布时间】:2011-08-15 23:29:03
【问题描述】:

我没有运气将 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


【解决方案1】:

您可以轻松地制作一个 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);

【讨论】:

  • 很遗憾,一个轻率的评论损害了一个体面的答案。我已经编辑了。
  • 无论如何,这个答案作为一个快速解决方案可能是救命稻草,但最好不要这样重写。特别是对于电子商务网站。请阅读此文档nginx.com/resources/wiki/start/topics/examples/…
【解决方案2】:

还没有测试过,但是看起来比the oneAlex提到的要好。

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

关于htaccess to nginx converter

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

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

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

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

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

【讨论】:

  • winginx.com/htaccess 不起作用
  • 昨天刚注意到它现在已经关闭了,是的。希望它会重新上线。
【解决方案3】:

将 Apache .htaccess 翻译成 Nginx 重写工具的在线工具包括:

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

【讨论】:

  • 这似乎对我不起作用,但确实如此:winginx.com/htaccess
  • 两者都没有为我工作。我尝试了一个非常基本的重写规则。
  • 你能告诉我,我应该在哪里使用输出?在 php 文件中?我使用了this,它给了我一个包含if的输出,现在我想知道我应该如何使用它?
【解决方案4】:

使用这个: http://winginx.com/htaccess

在线转换器,好方法又省时;)

【讨论】:

  • 如果你仔细观察,你会发现我在你之前发布了那个链接..
【解决方案5】:

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

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

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2018-07-10
    • 2017-08-02
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    相关资源
    最近更新 更多