【问题标题】:Capture variable nginx if statement捕获变量 nginx if 语句
【发布时间】:2022-01-23 04:30:23
【问题描述】:

我在一个网站上工作了很长时间,我想将旧的无用页面重定向到列出相关文章的类别页面。

我在我的谷歌搜索控制台上看到了以下网址:

https://www.example.com/Greece/sport%20?page=55

我想得到:

https://www.example.com/greece.html?page=55

我正在使用带有自定义脚本的 nginx,该脚本在服务器设置中强制指示必须为哪个域进行重定向。 我正在尝试找出方法,但下面的代码没有给我正确的重定向。 如果有人能给我提示,我将不胜感激。

location ^~  /(.*)/  {
  if ($host ~* ^(www\.)?(mysite\.com)$) {
     return 301 $scheme://$host/$1.html;
   }
   return 404;
 }

谢谢

【问题讨论】:

    标签: nginx redirect


    【解决方案1】:

    试试这个配置:

    server {
       ...
       server_name mysite.com www.mysite.com;
       
       location ~* ^(/[^/]+)/ {
           return 301 $scheme://$host$1.html$is_args$args;
       }
       ....
    }
    

    location块中的正则表达式可以根据需要进行修改。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2018-01-08
    • 2013-05-12
    • 2017-03-21
    • 1970-01-01
    相关资源
    最近更新 更多