Search code examples
apache.htaccessmod-rewritenginxwinginx

How to convert .htaccess to a nginx equivalent?


How to convert .htaccess configuration to nginx?

My .htacess:

Options -MultiViews
RewriteEngine On
RewriteBase /mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

I tried this:

nginx configuration

location /mvc/public/ {
if (!-e $request_filename){
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 [QSA,L];
}
}

But this did not work! Could someone help me?


Solution

  • The [QSA,L] is not nginx syntax - see this document for details.

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

    Similar behaviour can be accomplished with try_files rather than the if:

    location /mvc/public/ {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
    }
    

    See this document for details.