如何从 URL 中删除 .html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5730092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:07:43  来源:igfitidea点击:

How to remove .html from URL?

html.htaccess

提问by Dave

How to remove .htmlfrom the URL of a static page?

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

Also, I need to redirect any url with .htmlto the one without it. (i.e. www.example.com/page.htmlto www.example.com/page).

另外,我需要将任何网址重定向.html到没有它的网址。(即www.example.com/page.htmlwww.example.com/page)。

采纳答案by Dave

Thanks for your replies. I have already solved my problem. Suppose I have my pages under http://www.yoursite.com/html, the following .htaccessrules apply.

感谢您的回复。我已经解决了我的问题。假设我在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>

回答by binaryfunt

I think some explanation of Jon's answerwould 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$ / [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 thinkthat's correct.

认为这是正确的。

NOTE: When testing your .htaccessdo notuse 301 redirects. Use 302 until finished testing, as the browser will cache 301s. See https://stackoverflow.com/a/9204355/3217306

注意:测试时.htaccess不要使用 301 重定向。使用 302 直到完成测试,因为浏览器会缓存 301。见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

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

https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules

回答by starkeen

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

要从您的网址中删除 .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]

NOTE: If you want to remove any other extension, for example to remove the .php extension, just replace the htmleverywhere with phpin the code above.

注意:如果你想删除任何其他扩展名,例如去除PHP扩展,只需更换HTML与海内外PHP在上面的代码。

回答by Jon Skarpeteig

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

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

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

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

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

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

回答by ?ukasz Habrzyk

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 ^(.+)$ .html [L,QSA]

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

回答by Bradley Flood

You will need to make sure you have Options -MultiViewsas well.

你需要确保你也有Options -MultiViews

None of the above worked for me on a standard cPanel host.

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

This worked:

这有效:

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

回答by Anmol

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

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

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

回答by Akash Limbani

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

要从您的 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 ^(.*)$ .html [NC,L]

Thanks

谢谢

回答by HarsH

For those who are using Firebase hosting none of the answers will work on this page. Because you can't use .htaccessin Firebase hosting. You will have to configure the firebase.json file. Just add the line "cleanUrls": truein your file and save it. That's it.

对于使用 Firebase 托管的用户,此页面上没有任何答案。因为您不能.htaccess在 Firebase 托管中使用。您必须配置 firebase.json 文件。只需"cleanUrls": true在文件中添加该行并保存即可。就是这样。

After adding the line firebase.json will look like this :

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

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

回答by Jars?ter

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

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

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