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

发布于 2024-10-04 08:18:55 字数 5636 浏览 0 评论 0原文

我正在尝试将两个 .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
  }

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

I'm trying to convert two .htaccess files to nginx. The first one is in the root folder and contains this:

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]

The 2nd is located at /freshadmin/.htaccess and looks like this:

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]

Now I tried converting these with http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ , but I'm lost tbh. This is what I have so far and it gives me a 500 internal server error:

  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
  }

I know, it's quite a bit of code but I'd really appreciate if anybody got an idea!
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下客 2024-10-11 08:18:55

乍一看,我可以看到这里有一些问题。在 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
}

我建议注释掉所有内容,然后一一返回规则,修复出现的错误。

At a first glance I can see a few problems here. In nginx rewritten urls pass through locations and rewrites again, so this

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

will probably cycle and result in 500 error. Also, it is not clear what $1 means here, it can cause a problem too. This one should be changed to:

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

to avoid cycling and strange $1. There are probably more similar errors.

And more:

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

does nothing.

This

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

is handled different in 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
}

I suggest commenting out everything and then return rules one by one fixing errors as they come.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文