php-Nginx Laravel 4 URL查询重写

我正在尝试让Nginx拦截这样的网址:

http://website.dev/results?livesearch=bill+clinton

并显示如下:

http://website.dev/results/bill-clinton

我正在使用Laravel作为我的PHP框架.当我手动键入URL(http://website.dev/results/bill-clinton)时,我得到正确的页面.

我想做的是在文本输入字段中输入用户名.一旦他们单击提交,我希望该网址显示为http://website.dev/results/bill-clinton而不是http://website.dev/results?livesearch=bill clinton

我曾尝试在互联网上寻找一些帮助,但没有成功.

我的nginx虚拟服务器在下面.

server {

    listen       80;
    server_name  website.dev;

    access_log  logs/host.access.log  main;
    error_log   logs/host.error.log;
    rewrite_log     on;

    root   /path/to/www;
    index  index.php;

    #error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
        root   html;
    }

    location / {
        # Pretty URLs. Allows removal of "index.php" from the URL.
        # Useful for frameworks like Laravel or WordPress.
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Added cache headers for images,quick fix for cloudfront.
    location ~* \.(png|jpg|jpeg|gif)${
        expires 30d;
        log_not_found off;
    }

    # Only 3 hours on CSS/JS to allow me to roll out fixes during
    # early weeks.
    location ~* \.(js|css|ico)${
        expires 3h;
        log_not_found off;
    }

    # Turn off logging for favicon and robots.txt
    location = /robots.txt      { access_log off; log_not_found off; }
    location = /favicon.ico    { access_log off; log_not_found off; }   

    # Removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$/$1 permanent;
    }

    # Removes trailing "index" from all controllers.
    # Useful for frameworks like Laravel.
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$/$1 permanent;
    }

    # Unless the request is for a valid file (image,js,css,etc.),# send it to index.php
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$/index.php?/$1 last;
        break;
    }

    location ~ \.php${
        include                         fastcgi.conf;
        fastcgi_split_path_info         ^(.+\.php)(/.+)$;
        fastcgi_pass                    127.0.0.1:9000;
        fastcgi_index                   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
       deny  all;
    }
}
最佳答案
正则表达式和if =在nginx中几乎总是不好的.

在nginx中几乎所有您的正则表达式重写都将严重影响性能.

对于漂亮URL的初始路由,实际上可以使用以下命令:

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

Laravel非常聪明,可以查看$_SERVER [“ PATH_INFO”].它还可以处理斜杠.

路由

然后,您可以像这样路由您打算进行的搜索:

Route::any("/results/{search?}","Search@results"); // ? = optional

该表示法是Class @ method.它不是静态的.

在app / controllers / Search.php中,您将具有以下内容:

<?php

class Search extends BaseController {

    public function results($search = null) {
        if (!$search) {
            if (Input::has("q")) {
                // This is where you'd do SEO cleanup to remove special chars.
                return Redirect::to("/results/" . Input::get("q"));
            }
        } else {
            // do stuff with the $search variable here
        }

    }

}

当您在nginx中进行重写时,实际上无论如何都会重定向用户. (通过301、302或308重定向).

您可以使用javascript避免这种额外的请求(在提交时将浏览器发送到/ request / search-term),并且可以节省大量请求,而不会影响使用noscript浏览的用户的体验.

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...