【问题标题】:How to hide .html extension from the website url如何从网站 url 隐藏 .html 扩展名
【发布时间】:2012-11-30 23:38:47
【问题描述】:

我知道以前有人问过这个问题,但有人知道隐藏 .html 扩展名的好方法吗?我已经尝试了许多代码和来自https://stackoverflow.com/ 的许多答案,但没有看到结果。那是你我再问你一次

我有一个静态网站,我想删除扩展程序来清理我的网址。我正在处理静态 html 文件。

在删除扩展之前,url 会显示为website.com/about.html

删除扩展后,它看起来像 website.com/about。希望这很清楚。

我有一个 .htaccess 文件,我尝试了许多代码,但它不起作用。下面是一些代码

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]

但是我没有看到任何结果...:(

【问题讨论】:

    标签: php apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    你的规则颠倒了(否则第一个会起作用):

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

    您的原始规则是RewriteRule ^(.*)\.html$ $1 [L],它转换为“如果请求的资源名称以.html 结尾,则向浏览器发出重定向,告诉它询问我们是否有任何没有HTML 的同名资源”。这会导致 404(因为 Apache 没有任何东西可用于 .html'less 资源)。

    通过将.html 切换到目标RewriteRule ^(.*)$ $1.html [L],我们将规则更改为“如果请求的资源名称不是磁盘上的文件或目录,则尝试重新路由请求(在内部,不要告诉浏览器)就好像它以.html"结尾。

    【讨论】:

      【解决方案2】:

      请注意,此类任务不需要复杂的(本质上)RewriteRules。

      mod_negotiation apache 模块(通常默认启用)通过Multivews 选项提供此类行为。

      如果服务器收到对 /some/dir/foo 的请求并且 /some/dir/foo 不存在,则服务器读取目录以查找所有名为 foo.* 的文件,并有效地伪造一个类型映射,该映射命名所有这些文件,为它们分配相同的媒体类型和内容编码,如果客户端按名称要求其中之一。然后它选择最符合客户要求的匹配项,并返回该文档。

      .即请求foo/bar 将服务于foo/bar,如果它是一个目录,并且将搜索foo/bar.htmlfoo/bar.txt 等如果不是。

      您只需要确保在当前上下文(例如目录)中激活此选项

      Options +Multiviews
      

      【讨论】:

        【解决方案3】:

        使用这个

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

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 2012-11-25
            • 1970-01-01
            • 2014-03-23
            • 2021-01-23
            • 2017-02-24
            • 1970-01-01
            • 2018-03-14
            • 1970-01-01
            • 2012-04-19
            相关资源
            最近更新 更多