当有人访问您的静态网站时如何删除.html

时间:2019-03-09 17:42:10

标签: html nginx

现在我有一个由nginx服务的静态网站。 示例页面:www.mydomain.com

如果我或任何人访问www.mydomain.com,则显示为www.mydomain.com/index.html。

如何防止尾随.html并最终显示www.mydomain.com/index?

我可以找到许多谈论类似问题的问题,但是所提出的解决方案均无效。

谢谢

2 个答案:

答案 0 :(得分:1)

删除index.html和.html有所不同。

对于index.html,您需要在htaccess文件中进行“重定向”

RewriteEngine On
RewriteBase /
# redirect html pages to the root domain
RewriteRule ^index\.html$ / [NC,R,L]

现在,.html的工作方式不同。如果添加以下内容

RewriteEngine On
RewriteRule ^/?about/?$ about.html [L]
RewriteRule ^/?otherlink/?$ otherlink.html [L]

然后,当您访问www.mydomain.com/about时,它将“阅读” www.mydomain.com/about.html。因此,您需要在htaccess文件中添加以上内容。检查是否有效(只需输入URL),然后将所有链接更改为不带文件扩展名的链接。

答案 1 :(得分:0)

您可以使用.htaccess文件将www.yourdomain.com/index.html重定向到www.yourdomain.com

Here is a guide to redirect and rewrite URLs using .htacess