【问题标题】:How to remove .html when someone visit your static website当有人访问您的静态网站时如何删除 .html
【发布时间】:2019-07-31 11:42:57
【问题描述】:

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

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

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

我可以找到很多关于类似问题的问题,但所提出的解决方案都没有奏效。

谢谢

【问题讨论】:

标签: html nginx


【解决方案1】:

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

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

【讨论】:

  • 这与我想要完成的不同。再比如:想把 www.mydomain.com/about.html 改成 www.mydomain.com/about。顺便试了一下.htaccess。
【解决方案2】:

删除 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 ),然后将所有链接更改为没有文件扩展名的链接。

【讨论】:

  • htaccess 仅适用于 Apache 服务器。不适用于 Nginx。
猜你喜欢
  • 2017-05-24
  • 2020-03-22
  • 2019-05-31
  • 2019-05-22
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
相关资源
最近更新 更多