Charles Dahab
Charles Dahab

Reputation: 91

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?

Upvotes: 1

Views: 1420

Answers (1)

Richard Smith
Richard Smith

Reputation: 49782

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.

Upvotes: 2

Related Questions