【问题标题】:Converting .htaccess files to nginx将 .htaccess 文件转换为 nginx
【发布时间】:2021-02-07 00:49:52
【问题描述】:

我有下一个项目结构:项目目录和公用文件夹。在项目目录中,我有下一个 .htaccess 文件:

AddDefaultCharset utf-8
RewriteEngine On
RewriteRule (.*) public/$1

在公共文件夹中,我有 index.php 和另外一个 .htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [L,QSA]

如何将此规则重写为 nginx?

我尝试了在线资源,但它们没有帮助我: 字符集 utf-8;

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

【问题讨论】:

    标签: php .htaccess nginx


    【解决方案1】:

    试试这个:

    server {
        server_name example.com;
        root /path/to/files;
        charset utf-8;
        rewrite (.*) /public/$1;
        location /public/ {
            try_files /public$uri /public$uri/ /public/index.php?$uri$is_args$args;
        }
    }
    

    尽管如此,您只需要将您的root 指令设置为public 并不尊重您的“坏”.htaccess。 这也会更清洁/更安全:

    server {
        server_name example.com;
        root /path/to/files/public;
        charset utf-8;
        location / {
            try_files $uri $uri/ /index.php?$uri$is_args$args;
        }
    }
    

    【讨论】:

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