简体   繁体   English

如何隐藏网站网址的.html扩展名

[英]How to hide .html extension from the website url

I know this question has been asked before but does anyone know of a good way to hide .html extensions. 我知道之前已经问过这个问题,但有人知道隐藏.html扩展名的好方法。 I've tried many of codes & many of the answers from the https://stackoverflow.com/ but am not seeing the result. 我已经尝试了许多代码和https://stackoverflow.com/的许多答案,但我没有看到结果。 Thats y I ask u again 那是我再次问你

I've a static website and I wanted to remove the extension to clean my urls. 我有一个静态网站,我想删除扩展名来清理我的网址。 I was working with static html files. 我正在使用静态html文件。

Prior to removing the extension, the url would read website.com/about.html . 在删除扩展程序之前,该网址将显示为website.com/about.html

With the extension removed, it would look like website.com/about. 删除扩展程序后,它看起来像website.com/about。 Hope that was clear. 希望很清楚。

I've a .htaccess file and I tried many of the codes but it dosen't work. 我有一个.htaccess文件,我尝试了很多代码,但它不起作用。 Here are some codes 这是一些代码

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



RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\  [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]



RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]



RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]



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


RewriteBase /
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.html [nc]


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

however I am not seeing any results...:( 但是我没有看到任何结果...... :(

You have your rule reversed (the first one would work otherwise): 您的规则已被撤销(第一个规则将起作用):

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

Your original rule was RewriteRule ^(.*)\\.html$ $1 [L] which translates to "If the requested resource name ends with .html then issue a redirect to the browser telling it to ask us if we have anything at the same name without the HTML". 您的原始规则是RewriteRule ^(.*)\\.html$ $1 [L] ,它转换为“如果请求的资源名称以.html结尾,则向浏览器发出重定向,告诉它询问我们是否有任何相同的内容没有HTML的名称“。 This results in a 404 (since Apache doesn't have anything to serve for the .html 'less resource). 这导致404(因为Apache没有任何东西可以用于.html '较少的资源)。

By switching the .html to the destination RewriteRule ^(.*)$ $1.html [L] we change the rule to say "If the requested resource name is not a file or directory on disk then try to re-route the request (internally, don't tell the browser) as if it ended in .html ". 通过将.html切换到目标RewriteRule ^(.*)$ $1.html [L]我们将规则更改为“如果请求的资源名称不是磁盘上的文件或目录,则尝试重新路由请求(在内部,不要告诉浏览器)好像它以.html结尾“。

Note that you do not need complex (by nature) RewriteRules for such task. 请注意,对于此类任务,您不需要复杂(本质上)RewriteRules。

The mod_negotiation apache module, often enabled by default, provides such behavior with Multivews option. mod_negotiation apache模块(通常默认启用)通过Multivews选项提供此类行为。

If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. 如果服务器收到/ some / dir / foo的请求并且/ some / dir / foo不存在,那么服务器会读取名为foo。*的所有文件的目录,并有效地伪造一个类型映射,该映射将所有这些命名为如果客户端通过名称请求其中一个文件,则为它们分配相同的媒体类型和内容编码。 It then chooses the best match to the client's requirements, and returns that document. 然后,它会根据客户的要求选择最佳匹配,并返回该文档。

.ie requesting for foo/bar will serve foo/bar if it us a directory and will seak for foo/bar.html , foo/bar.txt and such etc if not. .IE请求foo/bar将成为foo/bar ,如果我们的目录,并将seak为foo/bar.htmlfoo/bar.txt和这样等如不。

You just need to ensure this option is activated in your current context (a Directory for example) 您只需要确保在当前上下文中激活此选项(例如,目录)

Options +Multiviews

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

use this 用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM