【问题标题】:Converting an ExpressionEngine rewrite rule for and NginX server为 NginX 服务器转换 ExpressionEngine 重写规则
【发布时间】:2011-01-16 18:28:00
【问题描述】:

我正在尝试将工作中的 ExpressionEngine 安装从 Apache 环境迁移到不同机器上的 NginX 环境。我在尝试将一些 .htaccess 重写转换为 NginX 时遇到了问题。

该网站使用multi language module,因此需要为每种其他语言自定义重写规则。

这是我的标准虚拟主机配置,它似乎让 ExpressionEngine 工作得很好(没有多语言模块):

server {
  listen        80;
  server_name   domain.co.uk www.domain.co.uk;
  root          /var/www/vhosts/domain.co.uk/http;

  # Redirects non-www to www
  if ($host = 'domain.co.uk') {
    rewrite ^/(.*) http://www.domain.co.uk/$1 permanent;
  }

  access_log    /var/www/vhosts/domain.co.uk/log/access.log;
  error_log     /var/www/vhosts/domain.co.uk/log/error.log;

  location / {
    index       index.html index.htm index.php;
    # Removes index.php from URLs
    if (!-e $request_filename) {
      rewrite ^/(.*)$ /index.php/$1 last;
    }
  }

  # Standard pass for all PHP files
  location ~ \.php$ {
    include       fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME /var/www/vhosts/domain.co.uk/http$fastcgi_script_name;
  }

  # This is where all the ExpressionEngine magic happens
  location ~ \.php($|/) {
    include       fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;

    set           $script $uri;
    set           $path_info "";

    if ($uri ~ "^(.+\.php)(/.+)") {
      set         $script $1;
      set         $path_info $2;
    }

    fastcgi_param SCRIPT_FILENAME /var/www/vhosts/domain.co.uk/http$script;
    fastcgi_param SCRIPT_NAME $script;
    fastcgi_param PATH_INFO $path_info;
  }
}

以上似乎工作得很好,并做了我想要的。多语言模块文档基于 Apache 设置。对于每种其他语言,它都需要一个具有自己的 htaccess 重写规则的目录 - 有点像这样:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.uk$
RewriteRule (.*) http://www.domain.co.uk/ar/$1 [R=301,L]

# Remove index.php
RewriteCond $1 !^(index\.php) [NC]
RewriteRule ^(.*)$ /de/index.php/$1 [L]

我通过添加以下内容重新创建了上述规则:

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

当我尝试访问 http://www.domain.co.uk/de/my_page 时,添加上述内容只会出现 404 错误页面。

所以,我想这可能与 fcgi_param SCRIPT FILENAME 有关,所以我将其更改为:(将 de 添加到路径末尾)

fastcgi_param SCRIPT_FILENAME /var/www/vhosts/spectrumhealthcare.co.uk/http/de$script;

当我访问http://www.domain.co.uk/de/my_page 时,现在执行此操作会给我一个No input file specified 错误。

我现在有点像一堵墙,所以真正帮助 SO 社区可以帮助我。你还没有让我失望:)。

【问题讨论】:

    标签: mod-rewrite url-rewriting nginx expressionengine


    【解决方案1】:

    我可以回答我自己的问题。看起来我的语言重写规则略有错误。应该是这样的:

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

    【讨论】:

      【解决方案2】:

      应该直截了当,在默认语言的 nginx 站点配置文件中,您可以执行以下操作:

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

      对于其他语言支持(比如 de/da/fr/ 等):

      location /de {
        try_files $uri $uri/ /de/index.php?q=$uri&$args;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-27
        • 2014-06-30
        • 2016-08-09
        • 2021-07-12
        • 2016-06-18
        • 2018-03-28
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多