skip to Main Content

How to convert below condition for nginx,

RewriteEngine on

RewriteRule ^([^.]+)/?$ index.php?$1 [L]
RewriteRule ^([^.]+)/([^.]+)/$ index.php?$1 [L]
RewriteRule ^([^.]+)/([^.]+)$ index.php?$1 [L]
RewriteRule ^([^.]+)/([^.]+)/([^.]+)$ index.php?$1 [L]
RewriteRule ^([^.]+)/([^.]+)/([^.]+)/$ index.php?$1 [L]
RewriteRule ^([^.]+)/([^.]+)/([^.]+)/([^.]+)$ index.php?$1 [L]
RewriteRule ^([^.]+)/([^.]+)/([^.]+)/([^.]+)/$ index.php?$1 [L]

I need to convert the apache .htaccess to Nginx configuration, if anyone knows the solution please help me, thanks.

Already I have converted .htaccess file to Nginx configuration by using some online tools, but it’s not working.

If I put the URL in the browser automatically downloaded the index.php file. I have checked info.php but fpm also working fine. I don’t know how to fix this issue.

here i have mention my Nginx conf file:

server {

   server_name example.com;
   root /home/example/public_html;
   index  index.php;

   access_log  /var/log/nginx/example/example-access.log;
   error_log  /var/log/nginx/example/example-error.log;

   location / {
  rewrite ^/([^.]+)/?$ /index.php?$1 break;
  rewrite ^/([^.]+)/([^.]+)/$ /index.php?$1 break;
  rewrite ^/([^.]+)/([^.]+)$ /index.php?$1 break;
  rewrite ^/([^.]+)/([^.]+)/([^.]+)$ /index.php?$1 break;
  rewrite ^/([^.]+)/([^.]+)/([^.]+)/$ /index.php?$1 break;
  rewrite ^/([^.]+)/([^.]+)/([^.]+)/([^.]+)$ /index.php?$1 break;
  rewrite ^/([^.]+)/([^.]+)/([^.]+)/([^.]+)/$ /index.php?$1 break;
}

   location ~ ^/(?:.htaccess) {
     deny all;
   }

 location ~ [^/].php(/|$) {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/run/php/php5.6-fpm-example.com.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }


}

2

Answers


  1. UPDATE 2022-03-18

    As pointed out by @Danila Vershinin the rewrite statements should end with last instead of break.


    You can try something like this

    location / {
      rewrite ^/([^.]+)/?$ /index.php?$1 break;
      rewrite ^/([^.]+)/([^.]+)/$ /index.php?$1 break;
      rewrite ^/([^.]+)/([^.]+)$ /index.php?$1 break;
      rewrite ^/([^.]+)/([^.]+)/([^.]+)$ /index.php?$1 break;
      rewrite ^/([^.]+)/([^.]+)/([^.]+)/$ /index.php?$1 break;
      rewrite ^/([^.]+)/([^.]+)/([^.]+)/([^.]+)$ /index.php?$1 break;
      rewrite ^/([^.]+)/([^.]+)/([^.]+)/([^.]+)/$ /index.php?$1 break;
    }
    

    or use a converter or another converter.

    Login or Signup to reply.
  2. The GetPageSpeed converter provides the correct result:

    server {
        server_name example.com;
        rewrite ^/([^.]+)/?$ /index.php?$1 last;
        rewrite ^/([^.]+)/([^.]+)/$ /index.php?$1 last;
        rewrite ^/([^.]+)/([^.]+)$ /index.php?$1 last;
        rewrite ^/([^.]+)/([^.]+)/([^.]+)$ /index.php?$1 last;
        rewrite ^/([^.]+)/([^.]+)/([^.]+)/$ /index.php?$1 last;
        rewrite ^/([^.]+)/([^.]+)/([^.]+)/([^.]+)$ /index.php?$1 last;
        rewrite ^/([^.]+)/([^.]+)/([^.]+)/([^.]+)/$ /index.php?$1 last;
    }
    

    Note the last directive. This is correct. It will make sure NGINX will start location search again to see which directives are applicable to rewritten URI /index.php. Then it finally ends up in your location ~ [^/].php(/|$) { ... } and directives for processing with PHP-FPM will apply.

    With break, things will not work, because there’s no new location search after rewriting URL. The URI stays /index.php but directives from location ~ [^/].php(/|$) { ... } will not be applied because NGINX didn’t "go to" apply directives from it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search