如何从URL中去除.html后缀?

184

如何从静态页面的URL中删除.html?

此外,我需要将任何带有.html的URL重定向到没有.html的URL(例如,将www.example.com/page.html重定向到www.example.com/page)。


2
“去掉 .html” 是指 “不需要存在 .html 吗?” - Lightness Races in Orbit
@Tomalak:是的,还要将带“.html”扩展名的重定向到不带扩展名的。我的问题是这会导致无限重定向。我的当前设置允许同时访问www.example.com/page.html和www.example.com/page,这对SEO并不友好。 - Dave
3
请参考以下链接:https://dev59.com/mFXTa4cB1Zd3GeqP6vge、https://dev59.com/3FXTa4cB1Zd3GeqP6ffd 和 http://stackoverflow.com/questions/5639367/mod-rewrite-recursive-loop。这些链接讨论了关于请求循环的原因、PHP中.htaccess文件实现美化URL的反向操作,以及mod_rewrite递归循环的问题。 - Lightness Races in Orbit
@Tomalak:感谢你的建议。阅读mod_rewrite的文档非常有帮助。 - Dave
另外请参考这个“如何移除HTML和PHP”的教程:https://helponnet.com/2020/02/04/remove-html-and-php-extension-with-htaccess-rewriterule-url-rewriting-tips/ - Amit Verma
18个回答

3

通过改进@amit-verma(https://dev59.com/aW025IYBdhLWcg3w6KSO#34726322)的答案,我希望为这个问题做出自己的贡献:

在我的情况下,我遇到了一个问题,即RewriteCond %{REQUEST_FILENAME}.html -f即使我不希望它触发(认为文件存在),它仍然会触发:

%{REQUEST_FILENAME}.html对于所有这些情况都给我/var/www/example.com/page.html

  • www.example.com/page(预期的)
  • www.example.com/page/(也很预期)
  • www.example.com/page/subpage(不是预期的)

所以它试图加载的文件(认为它是/var/www/example.com/page.html)是:

  • www.example.com/page => /var/www/example/page.html (正确)
  • www.example.com/page/ => /var/www/example/page/.html (不正确)
  • www.example.com/page/subpage => /var/www/example/page/subpage.html (不正确)

只有第一个指向一个现有文件,其他请求会给我返回500错误,因为它一直认为文件存在并重复添加.html

对于我来说,解决方案是将RewriteCond %{REQUEST_FILENAME}.html -f替换为RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f

这是我的整个.htaccess(我还添加了一个规则将用户从/index重定向到/):

# Redirect "/page.html" to "/page" (only if "/pages.html" exists)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} /(.+)\.html [NC]
RewriteRule ^(.+)\.html$ /$1 [NC,R=301,L]

# redirect "/index" to "/"
RewriteRule ^index$ / [NC,R=301,L]

# Load "/page.html" when requesting "/page" (only if "/pages.html" exists)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^ /%{REQUEST_URI}.html [QSA,L]

以下是帮助您理解所有情况的示例结果:
假设我在服务器上只有两个HTML文件(index.html和page.html),那么:
- www.example.com/index.html => 重定向到www.example.com - www.example.com/index => 重定向到www.example.com - www.example.com => 渲染 /var/www/example.com/index.html - www.example.com/page.html => 重定向到www.example.com/page - www.example.com/page => 渲染 /var/www/example.com/page.html - www.example.com/page/subpage => 返回404未找到 - www.example.com/index.html/ => 返回404未找到 - www.example.com/page.html/ => 返回404未找到 - www.example.com/test.html => 返回404未找到
不再出现500错误。
此外,为了帮助您调试重定向,考虑在浏览器中禁用网络缓存(因为旧的301重定向可能会被缓存在缓存中,这可能会导致一些麻烦):

Screenshot of Google Chrome's console, showing how to disable the "network cache"


感谢您提供的简要说明。建议将一处修正,将RewriteRule ^index$ / [NC,R=301,L]改为RewriteRule ^index.*$ / [NC,R=301,L],以避免加载index.php。现在,无论是index、index.php还是index.html都将重定向到主页。 - Ajmal PraveeN

2

首先创建一个 .htaccess 文件,并将其内容设置为 -

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

接下来,您需要从所有文件中删除“.html”后缀,例如test.html变成了test。如果您想从另一个文件打开文件,则还需要从文件名中删除“.html”后缀。


1

使用哈希标签。

可能不完全符合您的要求,但它解决了删除扩展名的问题。

假设您有一个保存为about.html的HTML页面,而您不想要这个烦人的扩展名,您可以使用哈希标签并重定向到正确的页面。

switch(window.location.hash.substring(1)){
      case 'about':
      window.location = 'about.html';
      break;
  }

将路由到yoursite.com#about会带您到yoursite.com/about.html。 我使用这个方法使我的链接更加简洁。


1
如果您有一个小型静态网站,HTML文件位于根目录中。
打开每个HTML文件并进行以下更改:
  1. href="index.html"替换为href="/"
  2. 删除所有本地链接中的.html。例如:“href="about.html"”应该看起来像“href="about"”。

0

为此,您需要将URL从/page.html重写为/page。 您可以在任何扩展名如.html .php等上轻松实现此功能

RewriteRule ^(.*).html$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

您将获得类似于以下的URL: example.com/page.html 转换为 example.com/page 请注意,下面两个URL都是可访问的。
如果您不想显示page.html,请尝试以下方法:

example.com/page.htmlexample.com/page

RewriteRule ^(.*).html$ $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

更多信息在这里


0
     RewriteEngine On
     RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*).html\ HTTP/
     RewriteRule .* https://example.com/html/%1 [R=301,L]
     RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*)\ HTTP/
     RewriteRule .* %1.html [L]

它可能有效,因为在我的情况下它有效。


0

为了从您的URL中删除.html扩展名,您可以在root/htaccess中使用以下代码:

#mode_rerwrite start here

RewriteEngine On

# does not apply to existing directores, meaning that if the folder exists on server then don't change anything and don't run the rule.

RewriteCond %{REQUEST_FILENAME} !-d

#Check for file in directory with .html extension 

RewriteCond %{REQUEST_FILENAME}\.html !-f

#Here we actually show the page that has .html extension

RewriteRule ^(.*)$ $1.html [NC,L]

谢谢


检查目录中扩展名为.html的文件是否存在 - 但条件是检查该文件不存在!您需要删除_CondPattern_前缀上的! - MrWhite

-4
RewriteRule /(.+)(\.html)$ /$1 [R=301,L] 

试试这个 :)不知道是否有效。


16
如果您不确定它是否有效,就不应将其发布为答案。 - user8678484

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接