2

I need to convert a .htaccess redirect to Nginx conf.

Here is the .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

Here is the relevant section of my Nginx config

location / {
   root   /var/www/vhosts/sitename/httpdocs;
   index  index.php index.html index.htm;
   try_files $uri $uri/ /index.php?$args;
}

According to the guide I found online I changed it to:

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

I get the error:

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
hemlock
  • 41
  • 4

2 Answers2

0

Try this:

location / {
    root /var/www/vhosts/sitename/httpdocs;
    index index.php index.html index.htm;
    if (!-e $request_filename){
       rewrite ^/(.*)$ /index.php;
    }
}

break is not only redundant, but also wrong, because if will make nginx terminate the rewriting process and land the request inside the current location (which is unaware about the php script handling).

try_files is a common approach when an engineer doesn't know where exactly his files are located, it's translated to nginx in 'look here, look there, looks everywhere, and help us god to find these files we're looking for'. I personally use try_files only in situations when a large set of files is relocated between two parts of the tree and files really could be in any of several locations, and this is not known decently.

Note that you also need a regexp location for *.php files, passing them to the fastcgi backend or any other one. This is beyond the scope of your question and of this answer; I hope you know what I mean.

drookie
  • 8,051
  • 1
  • 17
  • 27
0

Try something like:

location /wp-content/plugins/zendesk-for-woocommerce/api {
  if (!-e $request_filename){
  rewrite ^(.*)$ /wp-content/plugins/zendesk-for-woocommerce/api/index.php;
 }
}

Assuming that you have a working main location / block that will process the *.php requests like drookie mentioned, this block can go within. You may have the try_files defined within that main block already.

The $env_base within the rewrite will not work as it's not an NGINX variable. It would appear that the suggested conversion to NGINX was done through http://winginx.com/ or similar which doesn't account for Apache variables like the %{ENV:BASE} in .htaccess. Getting over this piece was the toughest in my troubleshooting.

Additionally, you won't want a break in that rewrite as NGINX will stop processing.

Obviously each setup is a bit different. After struggling to get this to work, I realized the improper conversion lead to various paths to troubleshooting and thinking %{ENV:BASE} could be a drop in which lead me in the wrong direction. Also, re-evaluating once I got over that hurdle, I wanted to scope it to the path specifically rather than keeping it wide open.

Hopefully this helps.

T-Rave
  • 1