如何从 URL 中删除 .html?

发布于 2024-11-02 11:30:16 字数 178 浏览 1 评论 0原文

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

另外,我需要将任何带有 .html 的网址重定向到不带它的网址。 (即 www.example.com/page.htmlwww.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

山色无中 2024-11-09 11:30:16

要从网址中删除 .html 扩展名,您可以在 root/htaccess 中使用以下代码:

RewriteEngine on


RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

注意:如果您想删除任何其他扩展名,例如删除 .php 扩展名,只需替换 htmlphp

另请参阅如何使用 htaccess 从 URL 中删除 .html.php< /a> .

To remove the .html extension from your urls, you can use the following code in root/htaccess :

RewriteEngine on


RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

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 .

梦毁影碎の 2024-11-09 11:30:16

我认为对乔恩的回答的一些解释是有建设性的。以下内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

检查如果指定的文件或目录分别不存在,则重写规则继续:

RewriteRule ^(.*)\.html$ /$1 [L,R=301]

但这是什么意思?它使用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:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

checks that if the specified file or directory respectively doesn't exist, then the rewrite rule proceeds:

RewriteRule ^(.*)\.html$ /$1 [L,R=301]

But what does that mean? It uses regex (regular expressions). Here is a little something I made earlier...
enter image description here

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/3217306

Update: I was slightly mistaken, . matches all characters except newlines, so includes whitespace. Also, here is a helpful regex cheat sheet

Sources:

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

拍不死你 2024-11-09 11:30:16

这应该适合你:

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

This should work for you:

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
挽手叙旧 2024-11-09 11:30:16

使用apache下的.htaccess,您可以像这样进行重定向:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

至于从url中删除.html,只需链接到不带.html的页面

<a href="http://www.example.com/page">page</a>

With .htaccess under apache you can do the redirect like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

As for removing of .html from the url, simply link to the page without .html

<a href="http://www.example.com/page">page</a>
樱娆 2024-11-09 11:30:16

您需要确保您还拥有Options -MultiViews

在标准 cPanel 主机上,以上内容都不适合我。

这有效:

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

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:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
习惯成性 2024-11-09 11:30:16

对于那些使用 Firebase 托管的人来说,此页面上的任何答案都不起作用。因为您无法在 Firebase 托管中使用 .htaccess。您必须配置 firebase.json 文件。只需在文件中添加行 "cleanUrls": true 并保存即可。就是这样。

添加行后 firebase.json 将如下所示:

{
  "hosting": {
    "public": "public",
    "cleanUrls": true, 
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

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 :

{
  "hosting": {
    "public": "public",
    "cleanUrls": true, 
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}
¢好甜 2024-11-09 11:30:16

感谢您的回复。我已经解决了我的问题。假设我的页面位于http://www.yoursite.com/html >,以下 .htaccess 规则适用。

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*).html\ HTTP/
   RewriteRule .* http://localhost/html/%1 [R=301,L]

   RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*)\ HTTP/
   RewriteRule .* %1.html [L]
</IfModule>

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.

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*).html\ HTTP/
   RewriteRule .* http://localhost/html/%1 [R=301,L]

   RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*)\ HTTP/
   RewriteRule .* %1.html [L]
</IfModule>
若无相欠,怎会相见 2024-11-09 11:30:16

好问题,但似乎让人们感到困惑。认为 Dave(OP)在不使用 .html 扩展名的情况下保存 HTML 页面的人和认为他正常保存 HTML 页面的人之间的答案几乎是平等的(带有 .html),但希望 URL 不显示。虽然这个问题的措辞可以更好一点,但我认为他的意思很清楚。如果他保存没有 .html 的页面,他的两个问题(“如何删除 .html”)和(如何“用 .html 重定向任何 url”)将是完全相同的问题!所以这样的解释没有多大意义。此外,他的第一个评论(关于避免无限循环)和他自己的答案似乎证实了这一点。

因此,让我们首先重新表述问题并分解任务。我们想要完成两件事:

  1. 如果 .html 是所请求 URL 的一部分(例如 /page.html),则明显地删除它;
  2. 指向裁剪后的 URL(例如 / page)返回到实际文件(/page.html)。

做这两件事都没有什么困难。 (我们可以通过启用 MultiViews 来实现第二个。)这里的挑战是在不创建无限循环的情况下同时完成这两个任务。

戴夫自己的答案完成了工作,但它相当复杂,而且根本不可移植。 (对不起戴夫。)Łukasz Habrzyk 似乎已经清理了 Anmol 的答案,最后 Amit Verma 对他们两个都进行了改进。然而,他们都没有解释他们的解决方案如何解决根本问题——如何避免无限循环。据我了解,它们之所以有效,是因为 THE_REQUEST 变量保存来自浏览器的原始请求。因此,条件 (RewriteCond %{THE_REQUEST}) 仅触发一次。由于它不会在重写时触发,因此您可以避免无限循环的情况。但随后您将处理完整的 HTTP 请求 — GETHTTP 等等 — 这部分解释了本页上一些更丑陋的正则表达式示例。

我将提供另一种方法,我认为这种方法更容易理解。我希望这可以帮助未来的读者理解他们正在使用的代码,而不是仅仅复制和粘贴他们几乎不理解的代码并希望得到最好的结果。

RewriteEngine on

# Remove .html (or htm) from visible URL (permanent redirect)
RewriteCond %{REQUEST_URI} ^/(.+)\.html?$ [nocase]
RewriteRule ^ /%1 [L,R=301]

# Quietly point back to the HTML file (temporary/undefined redirect):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]

让我们来分解一下……

第一条规则非常简单。该条件匹配任何以 .html(或 .htm)结尾的 URL,并重定向到不带文件扩展名的 URL。这是一个永久重定向,用于指示裁剪后的网址是规范 一个。

第二条规则也很简单。仅当请求的文件名不是有效目录 (!-d) 时,第一个条件才会通过。仅当文件名引用添加了 .html 扩展名的有效文件 (-f) 时,第二个才会通过。如果两个条件均通过,则重写规则只是将“.html”添加到文件名中。然后奇迹就发生了……[END]。是的,这就是防止无限循环所需的一切。 Apache RewriteRule Flags 文档对此进行了解释:

使用[END]标志不仅终止当前轮的重写
处理(如[L]),但也防止任何后续重写
在每个目录 (htaccess) 上下文中进行处理。

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:

  1. Visibly remove the .html if it’s part of the requested URL (e.g. /page.html)
  2. Point the cropped URL (e.g. /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.

RewriteEngine on

# Remove .html (or htm) from visible URL (permanent redirect)
RewriteCond %{REQUEST_URI} ^/(.+)\.html?$ [nocase]
RewriteRule ^ /%1 [L,R=301]

# Quietly point back to the HTML file (temporary/undefined redirect):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]

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:

Using the [END] flag terminates not only the current round of rewrite
processing (like [L]) but also prevents any subsequent rewrite
processing from occurring in per-directory (htaccess) context.

荒路情人 2024-11-09 11:30:16

使用 .htaccess 重写静态 HTML 的 URL 通常不仅没有必要,而且还会影响网站的性能。启用 .htaccess 也是一个不必要的安全漏洞 - 将其关闭可以消除大量潜在问题。每个 .htaccess 文件的相同规则可以改为进入该目录的 部分,如果您随后设置 AllowOverride,它将提高性能无,因为它不需要检查每个目录中的 .htaccess 文件,并且更安全,因为攻击者无法在没有 root 访问权限的情况下更改虚拟主机配置。

如果您在 VPS 环境中不需要 .htaccess,您可以完全禁用它并从您的 Web 服务器获得更好的性能。

您需要做的就是将单个文件从如下结构移动

index.html
about.html
products.html
terms.html

到如下结构:

index.html
about/index.html
products/index.html
terms/index.html

然后,您的网络服务器将呈现适当的页面 - 如果您加载 /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 set AllowOverride 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:

index.html
about.html
products.html
terms.html

To a structure like this:

index.html
about/index.html
products/index.html
terms/index.html

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.

笑着哭最痛 2024-11-09 11:30:16

我使用此 .htacess 从我的 url 站点中删除 .html 扩展,请验证这是正确的代码:

    RewriteEngine on
RewriteBase /
RewriteCond %{http://www.proofers.co.uk/new} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.proofers.co.uk/new/$1 [R=301,L]

I use this .htacess for removing .html extantion from my url site, please verify this is correct code:

    RewriteEngine on
RewriteBase /
RewriteCond %{http://www.proofers.co.uk/new} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.proofers.co.uk/new/$1 [R=301,L]
っ左 2024-11-09 11:30:16

通过改进 @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 重定向到 /):

# 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]

这是一个结果示例,可以帮助您理解所有情况:

考虑到我的服务器上只有 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 with RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f

Here is my entire .htaccess (I also added a rule to redirect the user from /index to /) :

# 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]

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 to www.example.com
  • www.example.com/index => redirects to www.example.com
  • www.example.com => renders /var/www/example.com/index.html
  • www.example.com/page.html => redirects to www.example.com/page
  • www.example.com/page => renders /var/www/example.com/page.html
  • www.example.com/page/subpage => returns 404 not found
  • www.example.com/index.html/ => returns 404 not found
  • www.example.com/page.html/ => returns 404 not found
  • www.example.com/test.html => returns 404 not found

No 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 ????):

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

违心° 2024-11-09 11:30:16

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

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

接下来从所有文件中删除 .html,例如。 test.html 变成只是测试,如果你想从另一个文件打开一个文件,那么也从中删除 .html 并只删除文件名

first create a .htaccess file and set contents to -

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

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

从来不烧饼 2024-11-09 11:30:16

使用哈希标签。

可能不完全是您想要的,但它解决了删除扩展的问题。

假设您有一个 html 页面另存为 about.html 并且您不想要那个讨厌的扩展,您可以使用井号标签并重定向到正确的页面。

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

路由到 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.

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

Routing to yoursite.com#about will take you to yoursite.com/about.html. I used this to make my links cleaner.

复古式 2024-11-09 11:30:16

如果您有一个小型静态网站并且 HTML 文件位于根目录中。

打开每个 HTML 文件并进行以下更改:

  1. href="index.html" 替换为 href="/"
  2. 删除所有本地链接中的 .html。例如:“href="about.html"”应类似于“href="about"”。

If you have a small static website and HTML files are in the root directory.

Open every HTML file and make the next changes:

  1. Replace href="index.html" with href="/".
  2. Remove .html in all local links. For example: "href="about.html"" should look like "href="about"".
半寸时光 2024-11-09 11:30:16

要从 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]

谢谢

To remove the .html extension from your URLs, you can use the following code in 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]

Thanks

深白境迁sunset 2024-11-09 11:30:16

为此,您必须将 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 都可以访问

example.com/page.htmlexample.com/page
如果你不想显示 page.html
试试这个

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

更多信息此处

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

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

You will get a URL something like this:
example.com/page.html to example.com/page
Please note both URLs below will be accessible

example.com/page.html and example.com/page
If you don't want to show page.html
Try this

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

More info here

爱*していゐ 2024-11-09 11:30:16
     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]

它可能会起作用,因为它适用于我的情况

     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]

it might work because its working in my case

记忆消瘦 2024-11-09 11:30:16
RewriteRule /(.+)(\.html)$ /$1 [R=301,L] 

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

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

Try this :) don't know if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文