使用nginx重写URL我的重写不起作用

时间:2017-09-12 20:10:58

标签: nginx

我正在使用nginx学习网址重写,并想知道为什么以下示例无法正常工作

我有以下网址

http://example.com/listings.php?city=Fayetteville

我有以下网址重新排列规则

rewrite ^/listings\.php\?city=(.*)$ http://google.com permanent;

我在访问http://example.com/listings.php?city=Fayetteville时应将URL更改为google.com,但没有任何反应。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。你会使用类似下面的东西

location = /listings.php {
    if ($arg_city) {
      rewrite ^(.*)$ /$arg_city;
   }
}

修改-1

您可以在配置中处理它,如下所示

server {
        listen 80;
        listen [::]:80;

        root /var/www/html/example.com;
        index index.html index.htm index.nginx-debian.html index.php;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                set $test '';
            if ($request_uri ~* "^/listing\.php") {
                    set $test "listing";
                }

                if ($arg_city) {
                    set $test "$test-city";
                }

                if ($test = "listing-city")
                {
                    return 301 /$arg_city;
                }

                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

}