如何使用 Apache mod_rewrite 隐藏 .html 扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1992183/
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-09-13 18:33:29  来源:igfitidea点击:

How to hide the .html extension with Apache mod_rewrite

apachemod-rewriteurl-rewritingfriendly-url

提问by Dave Everitt

I have a small number of static sites where I simply want to hide the .html extension:

我有少量的静态站点,我只想隐藏 .html 扩展名:

  • the url /foofetches the static file /foo.html
  • the browser still displays the url /foo
  • url/foo获取静态文件/foo.html
  • 浏览器仍然显示网址 /foo

The client can then send out bookmarks in the style mydomain.com/foorather than mydomain.com/foo.html.

然后,客户端可以发送样式mydomain.com/foo而不是 的书签mydomain.com/foo.html

It sounds very simple, and I've used mod_rewritehappily before (say with WordPress or for redirects), but this is proving much harder to crack that I thought. Perhaps I'm missing something really obvious, but I can't find a solution anywhere and I've been at it all day!

这听起来很简单,我以前用得很mod_rewrite开心(比如使用 WordPress 或重定向),但事实证明,这比我想的更难破解。也许我遗漏了一些非常明显的东西,但我在任何地方都找不到解决方案,而且我已经研究了一整天!

We run our own server, so this can go wherever is the best place.

我们运行我们自己的服务器,所以这可以去任何最好的地方。

Addendum

附录

The solution checked below worked fine. Then after running the site awhile I noticed two problems:

下面检查的解决方案工作正常。然后在运行该网站一段时间后,我注意到两个问题:

  1. all pages began to appear unstyled. I reloaded, cleared the cache, etc., but still no-style. I've had this trouble before, and can't locate the source.

  2. There's a directory AND an html file named 'gallery', so the /gallery link shows a directory listing instead of the html file. I should be able to sort that one, but further tips welcome :-)

  1. 所有页面都开始显得没有样式。我重新加载,清除缓存等,但仍然没有风格。我以前也遇到过这个问题,找不到来源。

  2. 有一个目录和一个名为“gallery”的 html 文件,因此 /gallery 链接显示目录列表而不是 html 文件。我应该能够对那个进行排序,但欢迎提供更多提示:-)

回答by Gumbo

Try this rule:

试试这个规则:

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

This will rewrite all requests that can be mapped to an existing file when appending a .html.

这将在附加.html.

回答by Stefan Profanter

The previous answers don't check if the requested path is a directory.

以前的答案不检查请求的路径是否是目录。

Here is the full rewrite condition which doesn't rewrite, if requested path is a directory (as stated by the original question):

这是不重写的完全重写条件,如果请求的路径是一个目录(如原始问题所述):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d          # is not directory
RewriteCond %{REQUEST_FILENAME}\.html -f     # is an existing html file
RewriteRule ^(.*)$ .html                   # rewrite index to index.html

To be SEO friendly and avoid double content, redirect the .html urls:

为了对 SEO 友好并避免重复内容,请重定向 .html 网址:

# Redirects domain.com/file.html to domain.com/file
RewriteCond %{REQUEST_FILENAME} !-d          # is not directory
RewriteCond %{REQUEST_FILENAME}\.html -f     # is an existing html file
RewriteCond %{REQUEST_URI} ^(.+)\.html$      # request URI ends with .html
RewriteRule (.*)\.html$ / [R=301,L]        # redirect from index.html to index

If you need the same for scripts take a look here: How can I use .htaccess to hide .php URL extensions?

如果您需要相同的脚本,请查看此处: How can I use .htaccess to hide .php URL extensions?

回答by Gael Lafond

The accepted solution do not works when the website is configured with a virtual host / document root.

当网站配置了虚拟主机/文档根目录时,已接受的解决方案不起作用。

There is the solution I used:

有我使用的解决方案:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

回答by Syed Priom

Look at this post http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/I haven't tried it yet but the demonstration seems pretty convincing.

看看这篇文章http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/我还没有尝试过,但演示似乎很有说服力。

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

回答by starkeen

To remove .htmlextension from .*.htmlrequests, you can use the following script in root/.htaccess:

要从请求中删除.html扩展.*.html,您可以在以下脚本中使用root/.htaccess

RewriteEngine On
RewriteBase /
#1) externally redirect "/file.html" to "/file"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
#2) rewrite  "/file" back to "/file.html"
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ .html [NC,L]

回答by Garret Wilson

Wow, I have seldom seen such an issue for which there exists so many "solutions" on the web, where people just throw up what "works for them" but for which few take time to read the documentation to figure out what it does. Many of the solutions given here don't work for virtual hosts, for example.

哇,我很少看到这样一个问题,网络上存在如此多的“解决方案”,人们只是抛出“对他们有用”的东西,但很少有人花时间阅读文档来弄清楚它的作用。例如,这里给出的许多解决方案不适用于虚拟主机。

After much cross-referencing and reading, I want to contribute my own solution that "works for me". Hopefully it works for you, too. I didn't create it from scratch; I was inspired by all the other contributions (even though most of them did not "work for me" without modification).

经过大量的交叉引用和阅读后,我想贡献我自己的“对我有用”的解决方案。希望它也适用于您。我不是从头开始创建的;我受到了所有其他贡献的启发(即使它们中的大多数不经修改就“不适合我”)。

RewriteEngine on

#if foo requested, return foo.html contents
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.html -f
RewriteRule ^(.*)$ .html [L]

#redirect foo.html to foo
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.+)\.html$  [R,L]

The [R]flag by default does a temporary (302) redirect; if you want a permanent redirect, use R=301in place of R.

[R]默认情况下,该标志会执行临时 (302) 重定向;如果你想有一个永久重定向,使用R=301到位R

回答by gahooa

Here is an example which allows us to store the file on disk as:

这是一个示例,它允许我们将文件存储在磁盘上:

foo.html.php

But in the browser, refer to it as

但在浏览器中,将其称为

foo.html

To make this work for you, I think you would just need to modify it a bit to match your existing requests, and check for an actual file in place with the .htmlextension.

为了使这项工作适合您,我认为您只需要稍微修改它以匹配您现有的请求,并检查是否有带有.html扩展名的实际文件。

 # These are so we do not need the .php extension
 RewriteCond %{REQUEST_FILENAME} (\.xul|\.html|\.xhtml|\.xml)$',
 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php -f',
 RewriteRule ^(.*)$ .php',