Mirko Brombin
Mirko Brombin

Reputation: 1012

Convert .htaccess rule to nginx

I'm trying to move a site that was born on apache server to a nginx server. I've the current .htaccess:

RewriteEngine on
RewriteRule ^(Personal)($|/) - [L]
RewriteRule ^mng/([-0-9a-zA-Z/%&]+)$ /index.php?aurl=$1 [L]
RewriteRule ^([-0-9a-zA-Z/%&]+)$ /index.php?url=$1 [L]

An online converter has converted these rules into:

location /mng {
   rewrite ^/mng/([-0-9a-zA-Z/%&]+)$ /index.php?aurl=$1 break;
}
location / {
   rewrite ^/([-0-9a-zA-Z/%&]+)$ /index.php?url=$1 break;
}

I added them to my nginx configuration but does not work, where am I wrong?
When I visit a page domain.ex/mng/index I can download the page.. This is my complete nginx conf:

server {
    listen 80;
    listen [::]:80;
    root /var/www;
    index index.php index.html;
    server_name domain.ex;
    location / {
       rewrite ^/([-0-9a-zA-Z/%&]+)$ /index.php?url=$1 break;
    }
    location /mng {
       rewrite ^/mng/([-0-9a-zA-Z/%&]+)$ /index.php?aurl=$1 break;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

Upvotes: 1

Views: 386

Answers (1)

Mirko Brombin
Mirko Brombin

Reputation: 1012

After several attempts, I solved the problem with the following rules:

location /mng/ {
    rewrite ^/(.*)/(.*)$ /?aurl=$1;
}
location / {
    rewrite ^/(.*)/(.*)$ /?url=$1;
}

Upvotes: 1

Related Questions