【问题标题】:Convert htaccess rules to nginx rules将 htaccess 规则转换为 nginx 规则
【发布时间】:2015-11-14 11:27:11
【问题描述】:

我有这些 apache (htaccess) 规则,我想将它们转换为 nginx 规则。我尝试了很多组合,但都是徒劳的。

你有以下规则:

RewriteRule ^candybooru/_images/([0-9a-f]{2})([0-9a-f]{30}).*$  /candybooru/images/$1/$1$2
RewriteRule ^candybooru/_thumbs/([0-9a-f]{2})([0-9a-f]{30}).*$  /candybooru/thumbs/$1/$1$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^candybooru/(.*)$ /candybooru/index.php?q=$1&%{QUERY_STRING}

【问题讨论】:

    标签: apache .htaccess mod-rewrite nginx


    【解决方案1】:

    也许你可以使用这个页面来转换规则:http://winginx.com/en/htaccess

    转换后的规则是:

    # nginx configuration
    location / {
    rewrite "^/candybooru/_images/([0-9a-f]{2})([0-9a-f]{30}).*$" /candybooru/images/$1/$1$2;
    rewrite "^/candybooru/_thumbs/([0-9a-f]{2})([0-9a-f]{30}).*$" /candybooru/thumbs/$1/$1$2;
    }
    location /candybooru {
    rewrite ^/candybooru/(.*)$ /candybooru/index.php?q=$1&$query_string;
    }
    

    【讨论】:

    • 如果我已经定义了位置 / {},我假设我将在该位置添加前 2 条规则,然后为该 candybooru 创建另一个位置,对吗?
    • 是的,您可以通过这种方式定义许多位置和基于位置的重写;欲了解更多信息,您可以访问此页面:nginx.org/en/docs/http/ngx_http_core_module.html#try_files
    • 已经尝试了所有可用的在线转换器。生成的规则不同,它们都不起作用。我会继续挖掘。非常感谢您的建议!
    【解决方案2】:

    查看try_files。我没有对此进行测试,但它应该可以工作(或接近):

    location /candybooru/ {
        location ~ "^/candybooru/_(images|thumbs)/([0-9a-f]{2})([0-9a-f]{30})" {
            try_files /candybooru/$1/$2/$2$3 /candybooru/index.php?q=$2&$query_string;
        }
    }
    

    【讨论】:

    • 我将对其进行测试并回复您。希望它会起作用!谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 2017-07-15
    • 2015-09-09
    • 1970-01-01
    • 2014-11-07
    • 2018-09-01
    • 2014-06-30
    相关资源
    最近更新 更多