将.htaccess规则转换为nginx

时间:2017-02-24 09:42:22

标签: apache .htaccess mod-rewrite nginx

我正在尝试将出生在apache服务器上的站点移动到nginx服务器。 我现在的.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]

在线转换器已将这些规则转换为:

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

我将它们添加到我的nginx配置但不起作用,我错在哪里? 当我访问页面domain.ex / mng / index时,我可以下载页面.. 这是我完整的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;
    }
}

1 个答案:

答案 0 :(得分:1)

经过多次尝试,我用以下规则解决了问题:

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