【问题标题】:How to convert htaccess to nginx如何将 htaccess 转换为 nginx
【发布时间】:2021-06-30 04:00:00
【问题描述】:

我在将 htaccess 文件转换为 nginx 时遇到问题。也许有人可以帮助我。

我需要转换以下代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]

我试过了,但这根本不起作用:

    location / {
    try_files $uri $uri/ /index.php?$args;
}

【问题讨论】:

  • 有趣的是,您告诉我,似乎没有在线 htaccess-to-Nginx 转换器输出适用于您的情况的指令。我没有 Nginx 文化,所以我无能为力。

标签: regex .htaccess nginx mod-rewrite url-rewriting


【解决方案1】:

我发现这是可行的:

if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?$1;

}

但它安全吗?

【讨论】:

    【解决方案2】:
    RewriteRule ^(.*)$ index\.php/$1 [L,QSA]
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    

    (我假设您的 .htaccess 文件位于文档根目录中。)

    您的 Apache RewriteRule 指令将 URL 路径作为路径信息传递给 index.php 脚本,因此您的 Nginx 指令应该类似于以下内容:

    location / {
        try_files $uri $uri/ /index.php$uri$is_args$args;
    }
    

    $uri 已经包含斜线前缀。默认情况下,Apache 也传递查询字符串,但在 Nginx 中,您需要附加 $is_args$args。 (当$args(查询字符串)不为空时,$is_args 只包含?。)

    【讨论】:

    • 一般情况下是可以的。但我有一些网址,即 www.example.c o m/example.php 他们不使用这个。
    • “不工作”到底是什么意思?你有错误吗?是否重写为index.php?提供/example.php 是一个物理文件,那么上面应该可以正常工作。只有当/example.php 不存在时,它才会真正发挥作用。 (?)
    • 不工作意味着我收到 404 错误。 /example.php 是服务器上的物理文件。使用 Apache 和 htaccess 它可以工作。真奇怪
    • example.txt 这样的文件呢?我怀疑在您的服务器上处理.php 文件的方式有些不同(我猜)。如果上面的try_files 没有按预期工作,那么你所有的 CSS、JS 和图像(静态资源)也会被破坏。
    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 2018-07-10
    • 2017-08-02
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多