skip to Main Content

I have problems to convert a htaccess file to nginx. Maybe someone can help me.

I need the following code converted:

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

I tried this but this doesnt work at all:

    location / {
    try_files $uri $uri/ /index.php?$args;
}

2

Answers


  1. Chosen as BEST ANSWER

    I found out that this works:

    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php?$1;
    }
    

    But is it safe?


  2. RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    

    (I’m assuming your .htaccess file was in the document root.)

    Your Apache RewriteRule directive is passing the URL-path as path-info to the index.php script, so your Nginx directive should be something like the following instead:

    location / {
        try_files $uri $uri/ /index.php$uri$is_args$args;
    }
    

    $uri already contains the slash prefix. Apache also passes the query string through by default, but in Nginx you’ll need to append $is_args$args. ($is_args simply contains ? when the $args (query string) is non-empty.)

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