【问题标题】:how to exclude .html and add / at the end of the url in htaccess如何排除 .html 并在 htaccess 中的 url 末尾添加 /
【发布时间】:2022-01-25 11:55:35
【问题描述】:

我有这个 htaccess 代码删除了我的 URL 的 .html 扩展名,但我想在删除 .html 扩展名后在 URL 的末尾添加 /,我该怎么做? 这是我的代码:

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

【问题讨论】:

  • 末尾的斜线表示您的文件现在是目录而不是文件。你确定这是你想做的吗?

标签: apache .htaccess url mod-rewrite seo


【解决方案1】:

你可以用这个

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/?$ $1.html [NC,L]

但是,此规则不会从 URL 中删除 .html 扩展名,它只是使您可以在浏览器地址栏中键入 exampe.com/file 而不是 example.com/file.html。如果您想从 URL 中删除 html 扩展名,您可以使用以下完整规则:

RewriteEngine On
#redirect /file.html to /file
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ %1/ [L,R=301]
#internally map /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/?$ $1.html [NC,L]

【讨论】:

  • 我将 url 从 example.com/file.html 更改为 example.com/file/ 而不是 example.com/file 并且很抱歉将答案标记为正确
  • @orenzofoods 规则已经做到了。请在您的服务器上进行测试。它在 Apache 上对我来说工作得非常好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-14
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
相关资源
最近更新 更多