Convert .htaccess to Nginx Directives

Viewed 76

My website was working fine with Apache .htaccess rules. Now I have sifted it to nginx. I need help to convert following apache rules to nginx directives/configurations

Apache .htaccess rules

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /public
 RewriteRule ^(/)?$ index.php/$1 [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

and my current nginx config file:

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm;
        server_name myhost.com;
        location / {
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        }
}
1 Answers

This is how I updated my nginx config file and it worked

server {
        listen 80;
        root /var/www/html/public;
        index index.php index.html index.htm;
        server_name myhost.com;

        if (!-e $request_filename) {
           rewrite ^/(.+)$ /index.php?url=$1 last;
           break;
        }    

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        }
}
Related