【问题标题】:nginx rewrite nice urls with homemade CMSnginx 用自制的 CMS 重写漂亮的 url
【发布时间】:2013-05-06 06:09:44
【问题描述】:

我有一个自制的 CMS,为我继承的网站提供服务。我不太熟悉 nginx 重写规则,尽管我可以设置微小的 URL。这是我配置的相关部分:

*location / {
        index index.php index.html;
        root /var/www/www.valami.hu;
        try_files $uri $uri/ @seo;
    }
location @seo {
rewrite ^/([a-z]+)$ /index.php?oldal=$1 last;
break;
}*

问题是该站点有一个位于 blogspot.com 上的博客,并且该博客的内容是从那里获取的。所以我需要帮助的是这种 URL 的规则:

http://www.valami.hu/index.php?oldal=blog&options=2012/01/some-title-here.html

所以,这样就好了:

http://www.valami.hu/blog/2012/01/some-title-here

最重要的是第一条规则也应该有效,因为它更常用。

【问题讨论】:

  • 我认为它应该适用于某种 IF,我的意思是如果第二个参数缺少第一个规则应该应用。其他我问过的... :-)

标签: nginx seo rewrite


【解决方案1】:

看来我们只有 2 个案例,/blog 和非 /blog,我会写 2 个位置块

location ~ ^/blog/(.*) {
    try_files $uri /index.php?oldal=blog&options=$1;
}
location ~ /(.*) {
    try_files $uri /index.php?oldal=$1;
}

我会在第二个位置只使用/$request_uri,但这会在olda1 中放置一个前面的/,如果这对你来说不重要,那么我更喜欢这种方法,因为它不涉及正则表达式。

关于index index.php index.html;root /var/www/www.valami.hu;,当然如果可能的话,最好将它们移动到服务器块而不是位置块。

【讨论】:

    【解决方案2】:

    这实际上是微不足道的。边看边学!

     location / {
        try_files $uri $uri/ @site;
     }
     location @site {
        rewrite ^/blog/(.+)$ /index.php?oldal=blog&options=$1 last;
        rewrite ^(.+)$ /index.php?oldal=$1 last;
     }
    

    顺序决定一切。您也可以通过删除last 标志并重定向到/blog 并明确设置选项查询字符串参数来实现。如果需要,则不需要。

    【讨论】:

    • 好吧,这可能是微不足道的,但不起作用......所以,如果我用 valami.hu/blog/2012/01/some-title-here 尝试它,它会给我 404,尽管这种格式有效:valami.hu/index.php?oldal=blog&options=2012/01/…
    • 您将位置块放在正确的服务器中并删除您拥有的旧位置/块,对吗? nginx 中的订单事项。
    • 您好!是的,配置中的位置很好。位置 / { 索引 index.php index.html;根 /var/www/www.kettlebelledzes.hu; try_files $uri $uri/ @seo; } 位置@seo { 重写 ^/blog/(.*)$ /?oldal=blog&options=$1 last;最后重写 ^/(.*)$ /index.php?oldal=$1;我将加号更改为星号,它可以正常工作!!!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2015-11-08
    相关资源
    最近更新 更多