【问题标题】:How to create SEO url for Nginx如何为 Nginx 创建 SEO url
【发布时间】:2015-12-10 14:03:04
【问题描述】:

我在将以下 htaccess 规则转换为 nginx 时遇到问题。我的网站是用 PHP 编写的。

重写引擎开启

重写规则 ^([a-zA-Z0-9-/]+).html$ fullstory.php?url=$1

重写规则 ^([a-zA-Z0-9-/]+).html/$ fullstory.php?url=$1

我正在尝试将这个 URL http://example.com/blog/fullstory.php?url=african-vegetable-recipe.html 转换为 http://example.com/blog/african-vegetable-recipe.html

从 example.com/blog/african-vegetable-recipe.html 获取“african-vegetable-recipe.html”并用作查询字符串。

nginx 可以处理这个吗?

【问题讨论】:

    标签: .htaccess nginx rewrite


    【解决方案1】:

    你在使用 php-fpm 吗?如果是这样,那么您不需要花时间重新编写 url。在 Nginx 中,您可以控制传递给 PHP 的参数。

    以下 Nginx 位置块会将所有匹配的请求传递给 fullstory.php,原始请求仍将出现在 $_SERVER['REQUEST_URI'] 中,您可以在其中解析它以获取所需的任何信息,而无需更改重写规则未来。

    location ~* ^([a-zA-Z0-9-/]+).html$ {  
        include        fastcgi_params;
        fastcgi_param  DOCUMENT_ROOT   /var/www/html/;
        fastcgi_param  SCRIPT_FILENAME /var/www/html/fullstory.php;
        fastcgi_pass   php-fpm;
    }
    

    fullstory.php 的确切路径显然需要更改以匹配您自己的系统。

    交替。将正则表达式的括号部分作为查询字符串的一部分传递给 PHP。在您的 server {} 块中使用此语句。

    rewrite ^([a-zA-Z0-9-/]+).html /fullstory.php?url=$1;
    

    【讨论】:

    猜你喜欢
    • 2012-12-03
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2015-11-03
    • 1970-01-01
    • 2020-12-11
    相关资源
    最近更新 更多