redirect - nginx redirect :/*/to/*. html (add .html to url)

标签 redirect nginx url-rewriting nginx-location

我想在每个以“/”结尾的文档中添加“.html”,但主页除外。

我尝试了一些不同的方法,使用 nginx 重写和返回 301,但我没有让它工作。附上我做的最后一个版本,它正在做/*/.html 但第二个/不应该在那里。

location / {
    try_files $uri $uri/ =404;
    return 301 $scheme://$host$request_uri.html;
}

我正在寻找的解决方案:

root: domain.com 应该作为 www.domain.com 交付

文件应该被重定向 (301) 到 *.html 版本

  • domain.com/abc/到 domain.com/abc.html
  • domain.com/dir1/abc/到 domain.com/dir1/abc.html

最佳答案

您将需要使用正则表达式来捕获尾随 / 之前的 URI 部分,以便消除它。一种解决方案是使用带有 try_files 语句的命名位置。

例如:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^(.+)/$ $1.html permanent;
}

参见 this document了解更多。

关于redirect - nginx redirect :/*/to/*. html (add .html to url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48043048/

相关文章:

php - laravel 构造函数重定向

.htaccess - 可以在 htaccess 中包含单独的文件以获取重定向列表吗?

.htaccess |如果有特定参数则不重定向

apache - 为 SEO 和漂亮的 URL 重写 map

url - 如何抽象 URL?

.htaccess - 在 .htaccess 中将 .com 重定向到 .org

ajax - 在javascript中获取iframe重定向url

python-requests 发出 GET 而不是 POST 请求

django - 权限被拒绝访问静态文件 Nginx +uwsgi +Django

nginx - 回退 NGINX 位置