【问题标题】:How to redirect index.html to /如何将 index.html 重定向到 /
【发布时间】:2019-01-30 15:02:07
【问题描述】:

如何将每个 index.html 文件重定向到 / 例如重定向这个 URL:https://www.example.com/contact/index.htmlhttps://www.example.com/contact/

注意:我不想重定向到根目录,我想重定向到同一目录只需删除 index.html 提前感谢您的宝贵时间

【问题讨论】:

标签: html .htaccess url redirect url-redirection


【解决方案1】:

假设您使用的是 Apache,/contact 文件夹下的 .htaccess 文件中的以下几行就可以解决问题:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/index.html$   %{CONTEXT_PREFIX}/folder/$1/        [R=301,L]
RewriteRule ^index.html$   %{CONTEXT_PREFIX}/folder/    [R=301,L]

在您的示例中,foldercontact

之前我建议使用以下规则作为上述两个的快捷方式:

RewriteRule ^(.*)index.html$   %{CONTEXT_PREFIX}/folder/$1       [R=301,L]

但是,这有一个不想要的副作用,就是将 https://www.example.com/contact/myindex.html 重定向到 https://www.example.com/contact/my 之类的内容

注意:请注意不要造成重定向“循环”,如:

xxx/index.html -> xxx/ -> xxx/index.html -> xxx/ ...

【讨论】:

  • 嗨@Fedon Kadifeli 非常感谢您的回答。如果 URL 是这样的:example.com/contact/page-1/index.html 并重定向到 example.com/contact/page-1 您提供的代码将 example.com/contact/page-1/index.html 返回到 example.com/contact 谢谢
  • 你确定吗?在我的环境中,它按预期工作。
  • 嗨@Fedon Kadifeli 是的,这是正确的。重定向是从example.com/contact/page-1/index.htmlexample.com/contact,所以它跳过了一个目录。
  • 你能粘贴.htaccess文件的相关部分吗? (另外,检查 Web 服务器日志以查看正在执行重定向操作,而不是显示 Web 浏览器缓存。)
  • 嗨@Fedon Kadifeli 只有您提供的代码在.htaccess RewriteEngine On RewriteBase / RewriteRule ^(.*)/index.html$ %{CONTEXT_PREFIX}/$1/ [R=301,L ] 重定向仍然是从 example.com/contact/page-1/index.html 到 example.com/contact,因此它正在跳过一个目录。谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-05-30
  • 2017-06-26
  • 2012-01-26
  • 2013-02-11
  • 2011-09-09
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
相关资源
最近更新 更多