【问题标题】:How to set nginx friendly url with lang suffix?如何设置带有 lang 后缀的 nginx 友好 url?
【发布时间】:2017-02-07 11:20:38
【问题描述】:

我希望拥有多语言 (php/html) 网站...但我在 nginx/rewrite 方面很弱 http地址中有3个变量(ln -language, page, step)

我写https://testpage.org.pl/esp/home的时候有问题 "/esp/" - 不像 $ln 变量那样被识别...

我该如何解决?

这是我的 nginx 重写:

rewrite ^/([a-zA-Z0-9]+)$ /index.php?page=$1 last;
rewrite ^/([a-zA-Z0-9]+)/step-([1-3])$ /index.php?page=$1&step=$2 last;
rewrite "^/([a-z]{2})/$" /index.php?ln=$1 last;
rewrite "^/([a-z]{2})/([a-zA-Z0-9]+)$" /index.php?ln=$1&page=$2 last;
rewrite "^/([a-z]{2})/([a-zA-Z0-9]+)/step-([1-3])$" /index.php?ln=$1&page=$2&step=$3 last;

【问题讨论】:

  • 不确定esp 代表什么语言,但上面的重写语句是为包含两个字符基的 ISO 639-1 语言代码设计的。这些代码与 HTTP 请求和 HTML 标头中使用的代码相同。
  • 它是一个 hmtl/php 网站
  • 抱歉,我认为您的问题与人类语言有关。
  • 没问题...我认为问题标签已经足够了:)

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


【解决方案1】:

如果您的语言变量始终包含 2 或 3 个字符,您只需将包含语言变量的重写更改为:

rewrite "^/([a-z]{2,3})/$" /index.php?ln=$1 last;
rewrite "^/([a-z]{2,3})/([a-zA-Z0-9]+)$" /index.php?ln=$1&page=$2 last;
rewrite "^/([a-z]{2,3})/([a-zA-Z0-9]+)/step-([1-3])$" /index.php?ln=$1&page=$2&step=$3 last;

这将确保它只匹配长度在 2 到 3 之间的语言,并且在您的情况下可能会正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 2012-06-05
    • 2023-03-29
    • 2014-07-26
    • 2011-07-31
    • 2014-02-26
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多