我需要将.htaccessredirect转换为Nginx conf。
这是.htaccess
Redirect 301 /wp-content/plugins/zendesk-for-woocommerce/api/redirect-test1.html /wp-content/plugins/zendesk-for-woocommerce/api/redirect-test2.html RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L] DirectoryIndex index.html index.htm index.php index.shtml
这里是我的Nginxconfiguration的相关部分
location / { root /var/www/vhosts/sitename/httpdocs; index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; }
根据我在网上find的指南,我把它改为:
location / { root /var/www/vhosts/sitename/httpdocs; index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; if (!-e $request_filename){ rewrite ^(.*)$ /$env_base index.php break; } }
我得到的错误:
nginx: [emerg] invalid number of arguments in "rewrite" directive in /etc/nginx/conf.d/sitename.conf:19 nginx: configuration file /etc/nginx/nginx.conf test failed
尝试这个:
location / { root /var/www/vhosts/sitename/httpdocs; index index.php index.html index.htm; if (!-e $request_filename){ rewrite ^/(.*)$ /index.php; } }
break不仅是多余的,而且也是错误的,因为如果将使nginx终止重写过程并将请求着陆到当前位置(这不知道php脚本处理)。
当工程师不知道他的文件在哪里时, try_files是一种常见的方法,它被翻译成nginx在'看这里,看看那里,无处不在,并帮助我们的上帝find我们正在寻找这些文件' 。 我个人只在使用try_files的情况下,当大量的文件在树的两个部分之间重新定位,并且文件真的可能在几个位置中的任何一个中,并且这是不为人知的。
请注意,您还需要* .php文件的正则expression式位置,将它们传递给fastcgi后端或其他任何位置。 这超出了你的问题和这个答案的范围。 我希望你明白我的意思。
尝试像这样:
location /wp-content/plugins/zendesk-for-woocommerce/api { if (!-e $request_filename){ rewrite ^(.*)$ /wp-content/plugins/zendesk-for-woocommerce/api/index.php; } }
假设你有一个工作的主要位置/块将处理* .php请求,如drookie提到的,这个块可以进去。 您可能已经在该主块中定义了try_files。
重写中的$ env_base将不起作用,因为它不是NGINXvariables。 看起来,build议的转换为NGINX是通过http://winginx.com/或类似的方式完成的,它不考虑Apachevariables,如.htaccess中的%{ENV:BASE}。 克服这个问题是我的疑难解答中最为棘手的问题。
另外,你不会因为NGINX停止处理而想要重写。
显然,每个设置都有点不同。 在努力完成这项工作之后,我意识到不正确的转换会导致各种故障排除的途径,并认为%{ENV:BASE}可能会导致我错误的方向。 另外,一旦我跨过这个障碍重新评估,我想专门的范围,而不是保持开放。
希望这有助于。