将站点从Apache移动到Nginx的问题(mod_rewrite)

时间:2017-06-25 01:34:04

标签: php apache .htaccess mod-rewrite nginx

我正在将一些vhost从apache迁移到nginx。不幸的是,由于来自apache实例的.htaccess文件,我遇到了一些麻烦。

情况如此(apache):

DocumentRoot包含不同的文件夹,里面有项目。我可以毫无问题地访问其中大部分(通过www.example.com/appname)。

但在访问(主页面)www.example.com时,以下.htaccess文件会重定向用户。

/var/www/html/.htaccess:

Redirect /index.html https://example.com/example/

然后在/var/www/html/example/.htaccess中有另一个.htaccess

Options -Indexes IndexIgnore */* Options FollowSymlinks RewriteEngine on RewriteRule ^(.+)?$ web/$1

然后是/ var / www / html / example / web

中的下一个.htaccess

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php

如何将其转换为nginx位置块或块?我似乎无法做到这一点。在我上次检查时,客户端浏览器说重定向有问题。这是nginx vhost config:

请记住,我发现还有一些尝试,但到目前为止,我没有任何运气。非常感谢帮助!

谢谢你们

server {
  server_name example.com www.example.com;
  listen 80;

  root   /var/www/html;
  index index.php index.html index.htm;

  access_log /dev/stdout;
  error_log /dev/stdout info;

  # FIRST .htaccess FILE
  # redirect index.html to example.com/example
  # the first line seems to have worked, the 2nd one is not yet tested
  location /index.html { rewrite ^(.*)$ http://example.com/example/ redirect; }
  location /index.html { return $301 $scheme:http://example.com/example/$request_uri; }

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

  location /example {
    root /var/www/html;

    # try_files options: 
    # try_files $uri $uri/ /index.php?$args;
    # try_files $uri /index.php?url=$request_uri;

    # SECOND .htaccess FILE
    rewrite ^/(.+)?$ /web/$1;

    location /example/web {
      # THIRD .htaccess FILE

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

      # option 2
      #rewrite ^/(.*)$ /$1.php last;

      try_files $uri $uri/ /index.php$args;
    }

  }


  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass php5:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }


  # deny access to .htaccess files, if Apache's document root
  # ocurs with nginx's one
  #
  location ~ /\.ht {
      deny  all;
  }
}

1 个答案:

答案 0 :(得分:1)

检查一下:Apache mod_rewrite to Nginx rewrite rules
基本上,apache重写和nginx重写并不意味着同样的事情。您似乎正在使用nginx rewrite,您应该使用nginx try_files。 所以,你可以尝试做

try_files /web/$uri /web/$uri/ /web/index.php?$args

而不是

rewrite ^/(.+)?$ /web/$1;