首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有seo友好文件名的nginx清理url

带有seo友好文件名的nginx清理url
EN

Stack Overflow用户
提问于 2012-03-24 19:36:43
回答 2查看 8.9K关注 0票数 1

我希望实现以下在Apache下工作得很好的代码。这是为了更好的搜索引擎优化的网址。

示例URLS:

代码语言:javascript
复制
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处理。

在上面所有的网址中,"enter","know_your_gemstone“都是PHP脚本,紧跟其后的是PHP生成的用于搜索引擎优化的虚拟文件名。实际上"indian_astrology_horoscope_chart_prediction.html“文件名并不存在。在Apache中,我使用以下代码截取"enter / know_your_gemstone“等,而不用关心文件名的其余部分:

代码语言:javascript
复制
DefaultType application/x-httpd-php

在上述URL的最后,"gID-7“用于将变量传递给脚本以显示相应的内容。当这个URL显示动态内容时,它看起来像一个静态URL,可以很容易地被搜索引擎索引。这个变量解析已经在PHP中完成了,与Nginx无关。我相信这部分已经被称为漂亮的url /干净的url了。

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

我有一个运行ubuntu的VPS,我已经用php-fpm安装了nginx,而对于像index.htm / index.php这样的普通网址,安装工作正常。我不是正则表达式编写的专家,因此我在这个节骨眼上被卡住了。我在网上搜索了很多nginx博客/论坛,但都没有找到正确的解决方案。

我使用的是Nginx v1.1.17和php v5.3.6.13的最新开发版本。我还编译了额外的模块,如更多的头,缓存清除,memcache等。

在这方面的任何帮助都将不胜感激。先谢谢你...

EN

回答 2

Stack Overflow用户

发布于 2012-03-26 01:41:30

这对你来说应该是可行的:

代码语言:javascript
复制
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;
            ...
        }
    }
}

代理的替代方案:

代码语言:javascript
复制
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;
            ...
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2012-03-25 02:00:04

您所指的是mod_rewrite,它通常由内容管理系统用来将看似静态的文件URL重写为index.php动态URL。这可以在nginx中完成,也可以通过重写:

代码语言:javascript
复制
server {
    # port, server name, log config omitted

    location / {
        root   /path/to/your/cms;
        index  index.php index.html;  # replace index.php with your real handler php

        if (!-f $request_filename) {  # request is not a file
            rewrite  ^(.*)$  /index.php?q=$1  last;
            break;
        }
        if (!-d $request_filename) {  # request is not a dir
            rewrite  ^(.*)$  /index.php?q=$1  last;
            break;
        }
    }

    # sample fastcgi php config, to allow running of your index.php
    location ~ .php$ {
      fastcgi_pass   127.0.0.1:9090;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  /path/to/your/cms$fastcgi_script_name;
      fastcgi_param  QUERY_STRING     $query_string;
      fastcgi_param  REQUEST_METHOD   $request_method;
      fastcgi_param  CONTENT_TYPE     $content_type;
      fastcgi_param  CONTENT_LENGTH   $content_length;
    }

    # static files
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
        access_log        off;
        expires           30d;
    }

    error_page  404              /index.php;
 }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9851399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档