重写 URL 中的 GET 参数 NGINX

重写 URL 中的 GET 参数 NGINX

我想得到

http://example.com/?page=main

http://example.com/main

每当我使用它时,rewrite它都会直接从我的网络服务器下载 PHP 脚本。

以下是我所使用的:

rewrite ^/(\w+)$ /index.php?page=$1 break;
rewrite ^/(\w+)+\/$ /index.php?page=$1 break;

编辑:块的完整配置。

server {
    listen 80;

    root /var/www/csgodouble;
    index index.php index.html index.htm;

    server_name csgovulture.com;
    charset   utf-8;

    gzip on;
    gzip_vary on;
    gzip_disable "msie6";
    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/xml+rss;

    location / {
        rewrite ^/(\w+)$ /index.php?page=$1 break;
        rewrite ^/(\w+)+\/$ /index.php?page=$1 break;
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
    }

    location ~* \.(?:css|js)$ {
      expires 7d;
      access_log off;
      add_header Cache-Control "public";
    }

    location ~ /\.ht {
        deny  all;
    }

}

答案1

您正在使用break重写规则,禁止任何其他位置处理该请求,因此您的请求location ~ \.php$被跳过。

相关内容