带有 seo 友好文件名的 nginx 干净 url [英] nginx clean url with seo friendly file names

查看:12
本文介绍了带有 seo 友好文件名的 nginx 干净 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望实现以下在 Apache 下完美运行的功能.这样做是为了更好地对 URL 进行 SEO.

I wish to implement the following which is working perfectly under Apache. This is being done for better SEO of the URLs.

示例网址:

http://www.astrogyan.com/enter/indian_astrology_horoscope_chart_prediction.html
http://www.astrogyan.com/know_your_gemstone/gID-7/sani_planet_saturn_gemstone_blue_sapphire_neelam.html

我真正期待的是一个位置正则表达式,用于捕获根文件夹中所有无扩展名的 php 脚本,仅供 php-fptm 处理.

What I am exactly looking forward to is a location regex to catch all extension-less php scripts in the ROOT FOLDER ONLY for processing by php-fptm.

以上所有网址中的enter"、know_your_gemstone"都是PHP脚本,后面跟着的是PHP为SEO生成的虚拟文件名.实际上indian_astrology_horoscope_chart_prediction.html"文件名不存在.在 Apache 中,我使用以下内容拦截输入/知道你的宝石"等并且从不关心文件名的其余部分:

In all the above URLs "enter", "know_your_gemstone" are all PHP scripts and what follows them are dummy file names generated by PHP for SEO. Actually "indian_astrology_horoscope_chart_prediction.html" file name does not exist. In Apache, I use the following which intercepts "enter / know_your_gemstone" etc and never bothers about the rest of the file name:

DefaultType application/x-httpd-php

在上述 URL 的最后一个中,gID-7"用于将变量传递给脚本以显示适当的内容.虽然此 URL 显示的是动态内容,但该 URL 非常精巧,看起来像一个 STATIC URL,可以很容易地被搜索引擎索引.这个变量解析已经在PHP中完成了,与Nginx无关.我相信这部分已经被称为漂亮的网址/干净的网址.

In the last of the above URL, "gID-7" is used to pass a variable to the script to show appropriate content. While this URL is showing DYNAMIC content, the URL is so crafted that is looks like a STATIC URL which can be indexed by search engines easily. This variable parsing is done in PHP already and has nothing to do with Nginx. I beleive this part is already referred as pretty url / clean url.

我需要知道如何在 NGINX 下最好地实现它?我需要正则表达式来处理根文件夹中的所有脚本(无扩展名的文件)并忽略这些脚本名称之后的内容.如果这样的文件不存在,则考虑检查 URL 的其余部分,希望它是后跟文件名的有效目录.这个目录部分是可选的,对我目前的需要不是必需的.

I need to know how best can this be implemented under NGINX? I need the regex to process all scripts (extension less files) in ROOT FOLDER and ignore what follows after such script names. If such a file does not exist, then consider to check the rest of the URL hoping it to be a valid directory followed by a file name. This directory portion is optional and not essential to my present needs.

我有一个运行 ubuntu 的 VPS,我已经安装了 nginx 和 php-fpm,对于像 index.htm/index.php 这样的普通 URL,设置工作正常.我不是正则表达式的专业人士,因此我在这个时刻陷入困境.我在网上搜索了很多 nginx 博客/论坛,但都没有找到合适的解决方案.

I have a VPS running ubuntu where I have installed nginx with php-fpm and for normal URL's like index.htm / index.php the setup is working fine. I am not a pro in regex writing hence I am stuck up at this juncture. I searched online under many nginx blogs / forum but could not find the right solution.

我使用的是 Nginx v1.1.17 和 php v5.3.6.13 的最新开发版本.我还编译了其他模块,如更多标头、缓存清除、内存缓存等.

I am using the latest development version of Nginx v1.1.17 with php v5.3.6.13. I have also compiled additional modules like more header, cache purge, memcache etc.

对此的任何帮助将不胜感激.提前致谢...

Any help on this will be most appreciated. Thanks in advance...

推荐答案

这应该适合你:

server {
    listen 80;
    server_name example.com;
    root   /full/server/path/to/your/cms;
    index  index.php;

    location / {
        try_files $uri $uri/ /phphandler
    }

    location /phphandler {
        internal;
        # nested location to filter out static items not found
        location ~ .php$ {
            rewrite ^/([^/]*)(.*) /$1 break;
            fastcgi_pass   127.0.0.1:8080;
            ...
        }
    }
}

替代代理:

server {
    listen 80;
    server_name example.com;
    root   /full/server/path/to/your/cms;
    index  index.php;

    location / {
        try_files $uri $uri/ /phphandler
    }

    location /phphandler {
        internal;
        # nested location to filter out static items not found
        location ~ .php$ {
            rewrite ^/([^/]*)(.*) /$1 break;
            proxy_pass   127.0.0.1:8080;
            ...
        }
    }
}

这篇关于带有 seo 友好文件名的 nginx 干净 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆