如何从 URL 中删除 .html?
如何从静态页面的 URL 中删除 .html
?
另外,我需要将任何带有 .html
的网址重定向到不带它的网址。 (即 www.example.com/page.html
到 www.example.com/page
)。
How to remove .html
from the URL of a static page?
Also, I need to redirect any url with .html
to the one without it. (i.e. www.example.com/page.html
to www.example.com/page
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
要从网址中删除 .html 扩展名,您可以在 root/htaccess 中使用以下代码:
注意:如果您想删除任何其他扩展名,例如删除 .php 扩展名,只需替换 htmlphp 。
另请参阅如何使用 htaccess 从 URL 中删除
.html
和.php
< /a> .To remove the .html extension from your urls, you can use the following code in root/htaccess :
NOTE: If you want to remove any other extension, for example to remove the .php extension, just replace the html everywhere with php in the code above.
Also see this How to remove
.html
and.php
from URLs using htaccess .我认为对乔恩的回答的一些解释是有建设性的。以下内容:
检查如果指定的文件或目录分别不存在,则重写规则继续:
但这是什么意思?它使用regex(正则表达式)。这是我之前做的一些东西......
我认为这是正确的。
注意:测试
.htaccess
时不要使用 301 重定向。在测试完成之前使用 302,因为浏览器会缓存 301。请参阅https://stackoverflow.com/a/9204355/3217306更新:我有点错误,
.
匹配除换行符之外的所有字符,因此包括空格。另外,这里有一个有用的正则表达式备忘单来源:
http://community.sitepoint.com/t/what-does-this-mean-rewritecond-request-filename-fd/2034/2
https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules
I think some explanation of Jon's answer would be constructive. The following:
checks that if the specified file or directory respectively doesn't exist, then the rewrite rule proceeds:
But what does that mean? It uses regex (regular expressions). Here is a little something I made earlier...
I think that's correct.
NOTE: When testing your
.htaccess
do not use 301 redirects. Use 302 until finished testing, as the browser will cache 301s. See https://stackoverflow.com/a/9204355/3217306Update: I was slightly mistaken,
.
matches all characters except newlines, so includes whitespace. Also, here is a helpful regex cheat sheetSources:
http://community.sitepoint.com/t/what-does-this-mean-rewritecond-request-filename-f-d/2034/2
https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules
这应该适合你:
This should work for you:
使用apache下的.htaccess,您可以像这样进行重定向:
至于从url中删除.html,只需链接到不带.html的页面
With .htaccess under apache you can do the redirect like this:
As for removing of .html from the url, simply link to the page without .html
您需要确保您还拥有
Options -MultiViews
。在标准 cPanel 主机上,以上内容都不适合我。
这有效:
You will need to make sure you have
Options -MultiViews
as well.None of the above worked for me on a standard cPanel host.
This worked:
对于那些使用 Firebase 托管的人来说,此页面上的任何答案都不起作用。因为您无法在 Firebase 托管中使用
.htaccess
。您必须配置 firebase.json 文件。只需在文件中添加行"cleanUrls": true
并保存即可。就是这样。添加行后 firebase.json 将如下所示:
For those who are using Firebase hosting none of the answers will work on this page. Because you can't use
.htaccess
in Firebase hosting. You will have to configure the firebase.json file. Just add the line"cleanUrls": true
in your file and save it. That's it.After adding the line firebase.json will look like this :
感谢您的回复。我已经解决了我的问题。假设我的页面位于http://www.yoursite.com/html >,以下 .htaccess 规则适用。
Thanks for your replies. I have already solved my problem. Suppose I have my pages under http://www.yoursite.com/html, the following .htaccess rules apply.
好问题,但似乎让人们感到困惑。认为 Dave(OP)在不使用
.html
扩展名的情况下保存 HTML 页面的人和认为他正常保存 HTML 页面的人之间的答案几乎是平等的(带有.html
),但希望 URL 不显示。虽然这个问题的措辞可以更好一点,但我认为他的意思很清楚。如果他保存没有.html
的页面,他的两个问题(“如何删除 .html”)和(如何“用 .html 重定向任何 url”)将是完全相同的问题!所以这样的解释没有多大意义。此外,他的第一个评论(关于避免无限循环)和他自己的答案似乎证实了这一点。因此,让我们首先重新表述问题并分解任务。我们想要完成两件事:
.html
是所请求 URL 的一部分(例如/page.html
),则明显地删除它;/ page
)返回到实际文件(/page.html
)。做这两件事都没有什么困难。 (我们可以通过启用 MultiViews 来实现第二个。)这里的挑战是在不创建无限循环的情况下同时完成这两个任务。
戴夫自己的答案完成了工作,但它相当复杂,而且根本不可移植。 (对不起戴夫。)Łukasz Habrzyk 似乎已经清理了 Anmol 的答案,最后 Amit Verma 对他们两个都进行了改进。然而,他们都没有解释他们的解决方案如何解决根本问题——如何避免无限循环。据我了解,它们之所以有效,是因为
THE_REQUEST
变量保存来自浏览器的原始请求。因此,条件 (RewriteCond %{THE_REQUEST}
) 仅触发一次。由于它不会在重写时触发,因此您可以避免无限循环的情况。但随后您将处理完整的 HTTP 请求 —GET
、HTTP
等等 — 这部分解释了本页上一些更丑陋的正则表达式示例。我将提供另一种方法,我认为这种方法更容易理解。我希望这可以帮助未来的读者理解他们正在使用的代码,而不是仅仅复制和粘贴他们几乎不理解的代码并希望得到最好的结果。
让我们来分解一下……
第一条规则非常简单。该条件匹配任何以
.html
(或.htm
)结尾的 URL,并重定向到不带文件扩展名的 URL。这是一个永久重定向,用于指示裁剪后的网址是规范 一个。第二条规则也很简单。仅当请求的文件名不是有效目录 (
!-d
) 时,第一个条件才会通过。仅当文件名引用添加了 .html 扩展名的有效文件 (-f
) 时,第二个才会通过。如果两个条件均通过,则重写规则只是将“.html”添加到文件名中。然后奇迹就发生了……[END]
。是的,这就是防止无限循环所需的一切。 Apache RewriteRule Flags 文档对此进行了解释:Good question, but it seems to have confused people. The answers are almost equally divided between those who thought Dave (the OP) was saving his HTML pages without the
.html
extension, and those who thought he was saving them as normal (with.html
), but wanting the URL to show up without. While the question could have been worded a little better, I think it’s clear what he meant. If he was saving pages without.html
, his two question (‘how to remove .html') and (how to ‘redirect any url with .html’) would be exactly the same question! So that interpretation doesn’t make much sense. Also, his first comment (about avoiding an infinite loop) and his own answer seem to confirm this.So let’s start by rephrasing the question and breaking down the task. We want to accomplish two things:
.html
if it’s part of the requested URL (e.g./page.html
)/page
) back to the actual file (/page.html
).There’s nothing difficult about doing either of these things. (We could achieve the second one simply by enabling MultiViews.) The challenge here is doing them both without creating an infinite loop.
Dave’s own answer got the job done, but it’s pretty convoluted and not at all portable. (Sorry Dave.) Łukasz Habrzyk seems to have cleaned up Anmol’s answer, and finally Amit Verma improved on them both. However, none of them explained how their solutions solved the fundamental problem—how to avoid an infinite loop. As I understand it, they work because
THE_REQUEST
variable holds the original request from the browser. As such, the condition (RewriteCond %{THE_REQUEST}
) only gets triggered once. Since it doesn’t get triggered upon a rewrite, you avoid the infinite loop scenario. But then you're dealing with the full HTTP request—GET
,HTTP
and all—which partly explains some of the uglier regex examples on this page.I’m going to offer one more approach, which I think is easier to understand. I hope this helps future readers understand the code they’re using, rather than just copying and pasting code they barely understand and hoping for the best.
Let’s break it down…
The first rule is pretty simple. The condition matches any URL ending in
.html
(or.htm
) and redirects to the URL without the filename extension. It's a permanent redirect to indicate that the cropped URL is the canonical one.The second rule is simple too. The first condition will only pass if the requested filename is not a valid directory (
!-d
). The second will only pass if the filename refers to a valid file (-f
) with the .html extension added. If both conditions pass, the rewrite rule simply adds ‘.html’ to the filename. And then the magic happens…[END]
. Yep, that’s all it takes to prevent an infinite loop. The Apache RewriteRule Flags documentation explains it:使用
.htaccess
重写静态 HTML 的 URL 通常不仅没有必要,而且还会影响网站的性能。启用.htaccess
也是一个不必要的安全漏洞 - 将其关闭可以消除大量潜在问题。每个.htaccess
文件的相同规则可以改为进入该目录的
部分,如果您随后设置AllowOverride,它将提高性能无
,因为它不需要检查每个目录中的.htaccess
文件,并且更安全,因为攻击者无法在没有 root 访问权限的情况下更改虚拟主机配置。如果您在 VPS 环境中不需要
.htaccess
,您可以完全禁用它并从您的 Web 服务器获得更好的性能。您需要做的就是将单个文件从如下结构移动
到如下结构:
然后,您的网络服务器将呈现适当的页面 - 如果您加载
/about/
,它会处理该页面如/about/index.html
。不过,如果有人访问旧的 URL,这不会重写 URL,因此如果将其追溯应用于现有站点,则需要重定向。
Resorting to using
.htaccess
to rewrite the URLs for static HTML is generally not only unnecessary, but also bad for you website's performance. Enabling.htaccess
is also an unnecessary security vulnerability - turning it off eliminates a significant number of potential issues. The same rules for each.htaccess
file can instead go in a<Directory>
section for that directory, and it will be more performant if you then setAllowOverride None
because it won't need to check each directory for a.htaccess
file, and more secure because an attacker can't change the vhost config without root access.If you don't need
.htaccess
in a VPS environment, you can disable it entirely and get better performance from your web server.All you need to do is move your individual files from a structure like this:
To a structure like this:
Your web server will then render the appropriate pages - if you load
/about/
, it will treat that as/about/index.html
.This won't rewrite the URL if anyone visits the old one, though, so it would need redirects to be in place if it was retroactively applied to an existing site.
我使用此 .htacess 从我的 url 站点中删除 .html 扩展,请验证这是正确的代码:
I use this .htacess for removing .html extantion from my url site, please verify this is correct code:
通过改进 @amit-verma 的答案,我对这个问题做出了自己的贡献(https://stackoverflow.com/a/34726322/2837434 ) :
就我而言,我遇到了触发
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/页面/子页面
(不是预期的)所以它尝试加载的文件(相信是
/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
重定向到/
):这是一个结果示例,可以帮助您理解所有情况:
考虑到我的服务器上只有 2 个 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 个错误
Making my own contribution to this question by improving the answer from @amit-verma (https://stackoverflow.com/a/34726322/2837434) :
In my case I had an issue where
RewriteCond %{REQUEST_FILENAME}.html -f
was triggering (believing the file existed) even when I was not expecting it :%{REQUEST_FILENAME}.html
was giving me/var/www/example.com/page.html
for all these cases :www.example.com/page
(expected)www.example.com/page/
(also quite expected)www.example.com/page/subpage
(not expected)So the file it was trying to load (believing if was
/var/www/example.com/page.html
) were :www.example.com/page
=>/var/www/example/page.html
(ok)www.example.com/page/
=>/var/www/example/page/.html
(not ok)www.example.com/page/subpage
=>/var/www/example/page/subpage.html
(not ok)Only the first one is actually pointing to an existing file, other requests were giving me 500 errors as it kept believing the file existed and appending
.html
repeatedly.The solution for me was to replace
RewriteCond %{REQUEST_FILENAME}.html -f
withRewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
Here is my entire
.htaccess
(I also added a rule to redirect the user from/index
to/
) :Here is a result example to help you understand all the cases :
Considering I have only 2 html files on my server (index.html & page.html)
www.example.com/index.html
=> redirects towww.example.com
www.example.com/index
=> redirects towww.example.com
www.example.com
=> renders/var/www/example.com/index.html
www.example.com/page.html
=> redirects towww.example.com/page
www.example.com/page
=> renders/var/www/example.com/page.html
www.example.com/page/subpage
=> returns 404 not foundwww.example.com/index.html/
=> returns 404 not foundwww.example.com/page.html/
=> returns 404 not foundwww.example.com/test.html
=> returns 404 not foundNo more 500 errors ????
Also, just to help you debug your redirections, consider disabling the network cache in your browser (as old 301 redirections my be in cache, wich may cause some headaches ????):
首先创建一个 .htaccess 文件并将内容设置为 -
接下来从所有文件中删除 .html,例如。 test.html 变成只是测试,如果你想从另一个文件打开一个文件,那么也从中删除 .html 并只删除文件名
first create a .htaccess file and set contents to -
next remove .html from all your files eg. test.html became just test and also if you wanna open a file from another file then also remove .html from it and just file name
使用哈希标签。
可能不完全是您想要的,但它解决了删除扩展的问题。
假设您有一个 html 页面另存为
about.html
并且您不想要那个讨厌的扩展,您可以使用井号标签并重定向到正确的页面。路由到
yoursite.com#about
会将您带到yoursite.com/about.html
。我用它来使我的链接更清晰。Use a hash tag.
May not be exactly what you want but it solves the problem of removing the extension.
Say you have a html page saved as
about.html
and you don't want that pesky extension you could use a hash tag and redirect to the correct page.Routing to
yoursite.com#about
will take you toyoursite.com/about.html
. I used this to make my links cleaner.如果您有一个小型静态网站并且 HTML 文件位于根目录中。
打开每个 HTML 文件并进行以下更改:
If you have a small static website and HTML files are in the root directory.
Open every HTML file and make the next changes:
要从 URL 中删除 .html 扩展名,您可以在 root/htaccess 中使用以下代码:
谢谢
To remove the .html extension from your URLs, you can use the following code in root/htaccess :
Thanks
为此,您必须将 URL 从 /page.html 重写为 /page
您可以轻松地在任何扩展名(如 .html .php 等)上实现此功能,
您将获得如下 URL:
example.com/page.html 到 example.com/page
请注意,下面的两个 URL 都可以访问
更多信息此处
For this, you have to rewrite the URL from /page.html to /page
You can easily implement this on any extension like .html .php etc
You will get a URL something like this:
example.com/page.html to example.com/page
Please note both URLs below will be accessible
More info here
它可能会起作用,因为它适用于我的情况
it might work because its working in my case
试试这个:)不知道是否有效。
Try this :) don't know if it works.