RewriteRule到Nginx conf

时间:2018-12-04 10:35:36

标签: nginx

我有一个 .htaccess

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

我需要将其转换为nginx conf,这很简单,但我找不到。.

我尝试过:

server {
    server_name ****;
    root /path;

    index index.html index.php;
    listen 80;

    # set expiration of assets to MAX for caching
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        expires max;
        log_not_found off;
    }

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

    location ~* \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

但是它下载文件而不显示文件。我不明白吗?

如果我使用:

http://example.com/index.php?url=login

但不是:

http://example.com/login

1 个答案:

答案 0 :(得分:1)

好的,它可以使用:

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