.htaccess到nginx:重写或内部重定向循环

时间:2010-11-26 08:58:38

标签: .htaccess mod-rewrite nginx

我正在尝试将两个.htaccess文件转换为nginx。第一个是在根文件夹中,包含:

RewriteRule ^install/?$ install/index.php [L]
RewriteRule ^freshadmin/?$ freshadmin/$1 [L]
RewriteRule ^i/?$ library/phpThumb/index.php$1 [L]
RewriteRule ^img/(.+).jpg$ image.php?n=productimage&var1=$1 [L]
RewriteRule ^recommends/([A-Za-z0-9-\+]+)/?$ linkfowarder.php?linkname=$1 [L]
RewriteRule ^([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/?$ index.php?n=$1&var1=$2&var2=$3&var3=$4&var4=$5 [L]
RewriteRule ^([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/?$ index.php?n=$1&var1=$2&var2=$3&var3=$4 [L]
RewriteRule ^([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-]+)/?$ index.php?n=$1&var1=$2&var2=$3 [L]
RewriteRule ^([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/?$ index.php?n=$1&var1=$2 [L]
RewriteRule ^([A-Za-z0-9-\+]+)/?$ index.php?n=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteCond %{REQUEST_URI} !^/min/
RewriteCond %{REQUEST_URI} !^/install/
RewriteRule ^(.+\.(css|js))$ /min/index.php?f=$1 [L,NE]

第二个位于/freshadmin/.htaccess,看起来像这样:

RewriteRule ^styles/(.+)?$ templates/styles/$1 [L]
RewriteRule ^scripts/(.+)?$ templates/scripts/$1 [L]
RewriteRule ^icons/(.+)?$ templates/icons/$1 [L]
RewriteRule ^images/(.+)?$ templates/images/$1 [L]
RewriteRule ^ajax/(.+)?$ ajax/$1 [L]

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?controller=$1&action=$2&var1=$3&var2=$4&var3=$5&var4=$6 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?controller=$1&action=$2&var1=$3&var2=$4&var3=$5 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?controller=$1&action=$2&var1=$3&var2=$4 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?controller=$1&action=$2&var1=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?controller=$1&action=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?controller=$1 [L]

现在我尝试用http://www.anilcetin.com/convert-apache-htaccess-to-nginx/转换它们,但我已经失去了。这是我到目前为止,它给了我一个500内部服务器错误:

  location /freshadmin/ {
    index index.php index.html;
    rewrite ^/freshadmin/styles/(.+)?$ /freshadmin/templates/styles/$1 last;
    rewrite ^/freshadmin/scripts/(.+)?$ /freshadmin/templates/scripts/$1 last;
    rewrite ^/freshadmin/icons/(.+)?$ /freshadmin/templates/icons/$1 last;
    rewrite ^/freshadmin/images/(.+)?$ /freshadmin/templates/images/$1 last;
    rewrite ^/freshadmin/ajax/(.+)?$ /freshadmin/ajax/$1 last;
    rewrite ^/freshadmin/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /freshadmin/index.php?controller=$1&action=$2&var1=$3&var2=$4&var3=$5&var4=$6 last;
    rewrite ^/freshadmin/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /freshadmin/index.php?controller=$1&action=$2&var1=$3&var2=$4&var3=$5 last;
    rewrite ^/freshadmin/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /freshadmin/index.php?controller=$1&action=$2&var1=$3&var2=$4 last;
    rewrite ^/freshadmin/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /freshadmin/index.php?controller=$1&action=$2&var1=$3 last;
    rewrite ^/freshadmin/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /freshadmin/index.php?controller=$1&action=$2 last;
    rewrite ^/freshadmin/([A-Za-z0-9-]+)/?$ /freshadmin/index.php?controller=$1 last;
    rewrite ^/freshadmin/?$ /freshadmin/$1 last;
  }

  location / {
    index index.php index.html;
    try_files $uri $uri/ @handler;

    rewrite ^/install/?$ /install/index.php last;
    rewrite ^/i/?$ /library/phpThumb/index.php$1 last;
    rewrite ^/img/(.+).jpg$ /image.php?n=productimage&var1=$1 last;
    rewrite ^/recommends/([A-Za-z0-9-\+]+)/?$ /linkfowarder.php?linkname=$1 last;
    rewrite ^/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/?$ /index.php?n=$1&var1=$2&var2=$3&var3=$4&var4=$5 last;
    rewrite ^/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/?$ /index.php?n=$1&var1=$2&var2=$3&var3=$4 last;
    rewrite ^/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/([A-Za-z0-9-]+)/?$ /index.php?n=$1&var1=$2&var2=$3 last;
    rewrite ^/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/?$ /index.php?n=$1&var1=$2 last;
    rewrite ^/([A-Za-z0-9-\+]+)/?$ /index.php?n=$1 last;
    if (-f $document_root/$1){
      set $rule_10 1$rule_10;
    }
    if ($uri !~ "^/min/"){
      set $rule_10 2$rule_10;
    }
    if ($uri !~ "^/install/"){
      set $rule_10 3$rule_10;
    }
    if ($rule_10 = "321"){
      rewrite ^/(.+\.(css|js))$ /min/index.php?f=$1 last;
    }

  }

  location @handler {
    rewrite / /index.php;
  }

  location ~ \.php$ { ## Execute PHP scripts
        if (!-e $request_filename) {
          rewrite / /index.php last;
        } ## Catch 404s that try_files miss
        expires        off; ## Do not cache dynamic content
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_param  HTTPS $fastcgi_https;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
  }

我知道,这是相当多的代码,但如果有人有想法,我真的很感激! 感谢

1 个答案:

答案 0 :(得分:1)

乍一看,我可以看到一些问题。在nginx中,重写的url会通过位置并再次重写,所以这个

rewrite ^/freshadmin/?$ /freshadmin/$1 last;

可能会循环并导致500错误。此外,目前尚不清楚$ 1的含义是什么,它也可能导致问题。这个应改为:

rewrite ^/freshadmin/?$ /freshadmin/ break;

避免骑车和奇怪的$ 1。可能存在更多类似的错误。

还有更多:

rewrite ^/freshadmin/ajax/(.+)?$ /freshadmin/ajax/$1 last;

什么也没做。

RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteCond %{REQUEST_URI} !^/min/
RewriteCond %{REQUEST_URI} !^/install/
RewriteRule ^(.+\.(css|js))$ /min/index.php?f=$1 [L,NE]

在nginx中的处理方式不同:

location ~ ^/min/|^/install/ {} # capture these two cases and serve them with default root
location ~ \.(css|js)$ {
    try_files $uri /min/index.php?f=$uri
}

我建议评论所有内容,然后逐步修复规则,修复错误。