【问题标题】:Nginx Laravel 4 url query rewriteNginx Laravel 4 url​​ 查询重写
【发布时间】:2013-12-24 12:10:54
【问题描述】:

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

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

并让它显示为:

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

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

我要做的是让用户在文本输入字段中输入名称;一旦他们点击提交,我希望 url 显示为 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;
    }
}

【问题讨论】:

    标签: php url-rewriting nginx laravel friendly-url


    【解决方案1】:

    Regex 和 if = almost 在 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 浏览的人的体验。

    【讨论】:

    • 你需要在 Redirect::to("/results/" . Input::get("q"));所以它看起来像 "return Redirect::to("/results/" . Input::get("q"));"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 2020-07-24
    • 2011-12-07
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多