将nginx /index.html重定向到根目录,导致无限重定向



我想避免在 www.example.com 和 www.example.com/index.html 上访问相同的HTML页面,即我想将索引.html重定向到root。

这:

location = /index.html {
return 301 $scheme://www.example.com;
}

导致我ERR_TOO_MANY_REDIRECTS,重定向循环。

任何想法我可以改变什么来使其工作?

PS这是我的整个nginx会议

server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include /etc/nginx/snippets/ssl-example.com.conf;
include /etc/nginx/snippets/ssl-params.conf;
include /etc/nginx/snippets/letsencrypt-challenge.conf;
root /var/www/newsite;
index index.php index.html index.htm;
server_name www.example.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location = /index.html {
return 301 $scheme://www.example.com;
}
}

这不是对主题的详细处理,而是一个简化的解释,只是为了回答你的困境。答案是,你需要放弃做你正在做的事情的尝试。

Web 服务器只能提供特定文件,例如 xyz.html 文件。它们无法提供文件夹。

https://www.example.com/abc/index.html的调用是对 Web 根目录的 abc 文件夹中的索引.html文件的请求。另一方面,对https://www.example.com/abc的调用是对 Web 根目录的 abc 文件夹的请求,如前所述,无法提供服务。

但是,正如您所注意到的,第二个调用会导致https://www.example.com/abc/index.html被送达。这是因为 Web 服务器通常设置为当调用文件夹而不指定要服务的特定文件时,将生成指向该文件夹中的索引.html文件的重定向,并改为提供该文件。也就是说,Web服务器在内部将https://www.example.com/abc请求转换为https://www.example.com/abc/index.html请求。

这就是配置中的index index.php index.html index.htm;行的作用。它说"如果有对未指定文件的文件夹的请求,请改为提供索引.php文件。如果没有此类文件,请提供索引.html。如果没有这样的文件,则提供索引.htm文件。如果这也不存在,请扔一个适合"

问题是,然后您继续指示您的网络服务器将https://www.example.com/index.html请求重定向到https://www.example.com,网络服务器重定向回https://www.example.com/index.html然后再次重定向回https://www.example.com在无限循环中,直到网络服务器或您的浏览器最终放弃。

你说I want to avoid having the same HTML page accessible on www.example.com and www.example.com/index.html, i.e i want to redirect the index.html to root.问题是为什么?这样做绝对没有任何好处,正如您发现的那样,您最终会陷入重定向循环。

您可能正在尝试一些SEO内容,但这不适用于此处。

最新更新