Converting an ExpressionEngine rewrite rule for and NginX server

有些话、适合烂在心里 提交于 2019-12-08 13:09:45

问题


I'm trying to migrate a working ExpressionEngine installation from an Apache environment over to an NginX environment on a different box. I have come across a problem trying to convert some .htaccess rewrites to NginX.

The site uses the multi language module so needs a custom rewrite rule for every additional language.

This is my standard vhost config which seems to get ExpressionEngine working nicely (without the multi language module):

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;
  }
}

The above seems to work nicely and does what I want it to. The Multi Language Module documentation is based on an Apache setup. For each additional language it requires a directory with it's own htaccess rewrite rule - a little like this:

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]

I have recreated the above rule by adding:

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

Adding the above gets me no further than a 404 error page when I try and visit http://www.domain.co.uk/de/my_page.

So, I figured maybe this was something to do with the fcgi_param SCRIPT FILENAME so I changed that to: (added de to end of path)

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

Doing this now gives me a No input file specified error when I visit http://www.domain.co.uk/de/my_page.

I'm kind of at a brick wall now so really helping the SO community can help me. You haven't let me down yet :).


回答1:


I can answer my own question. Looks like I had language rewrite rule slightly wrong. Is should look like this:

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



回答2:


Should be straight forward, in nginx site config file for default language you do something like below:

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

For other language support (say de/da/fr/ etc):

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


来源:https://stackoverflow.com/questions/2262367/converting-an-expressionengine-rewrite-rule-for-and-nginx-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!