【问题标题】:Nginx rewrite url to attain clean urlNginx 重写 url 以获得干净的 url
【发布时间】:2014-07-29 00:08:48
【问题描述】:

我有一个在 nginx 上运行的具有以下 url 结构的站点

http://example.com/category.php?page=1&query=latest

现在我的目标是使用 nginx rewrite 将其转换为下面的一个

http://example.com/latest/1/

其中 1 是页码并且是动态的

获得干净 url 的确切规则是什么,

除此之外,我还需要一个规则,以防有人为页面提供负值,它将自动重定向到页码的正值。

现在我现有的配置如下


 server {
        server_name example.com;
        error_log    /data1/nginx/aggri.com.error.log;

        root /data1/aggri;
        index index.php;
        rewrite_log on;
        location ~ .php$ {

                 try_files $uri $uri/ /index.php;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                expires       0;
                 add_header    Cache-Control  public;
                set $skip_cache 0;
if ($request_uri ~* "^(.*/)index\.php$") {
        return 301 $1;
    }        }

        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|js|woff|css)$ {
                access_log off; log_not_found off; expires max;
        }

        location = /robots.txt { access_log off; log_not_found off; }
        location ~ /\. { deny  all; access_log off; log_not_found off; }
}

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    例子:

    location  ~ /category.php$ {
       if ( $args ~ ^page=[-]*([0-9]+)&query=([\w]+)$ ) {
          set $pg $1;
          set $q  $2; 
          rewrite (.*)  /$q/$pg/? permanent;
       }
    }
    

    【讨论】:

    • 它没用,下面是我的错误日志,启用了 nginx 重写日志; aggri/india/325/index.php" 找不到(2:没有这样的文件或目录)
    • 显示完整示例,您的请求。
    • 您好,我已经提到了我的完整 nginx 配置,以便更加清晰
    • 显示您重定向到 aggri/india/325/index.php 时的原始请求和您添加的规则
    • example.com/category.php?page=325&query=india 是我输入的原始请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2012-02-16
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多