Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从URL中删除.html?_Html_.htaccess - Fatal编程技术网

如何从URL中删除.html?

如何从URL中删除.html?,html,.htaccess,Html,.htaccess,如何从静态页面的URL中删除.html 另外,我需要将任何带有.html的url重定向到没有它的url。(即www.example.com/page.html到www.example.com/page)。使用apache下的.htaccess,您可以执行如下重定向: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)\.html$ /$

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


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

使用apache下的.htaccess,您可以执行如下重定向:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
index.html
about.html
products.html
terms.html
index.html
about/index.html
products/index.html
terms/index.html
至于从url中删除.html,只需链接到没有.html的页面即可

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


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

谢谢你的回复。我已经解决了我的问题。假设我有我的页面,下面的.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>

重新启动发动机
重写cond%{THE_REQUEST}^[A-Z]{3,9}\/html/(.*).html\HTTP/
重写规则。*http://localhost/html/%1 [R=301,L]
重写cond%{THE_REQUEST}^[A-Z]{3,9}\/html/(.*)\HTTP/
重写规则。*%1.html[L]

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

    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]

这应该适合您:

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

您还需要确保您有
选项-多视图

在标准的cPanel主机上,以上这些都不适用于我

这起到了作用:

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

我认为对这个问题作一些解释是有建设性的。以下是:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
检查指定的文件或目录是否不存在,然后执行重写规则:

RewriteRule ^(.*)\.html$ /$1 [L,R=301]
但这意味着什么?它使用。这是我之前做的一个小东西。。。

我认为这是正确的

注意:测试
.htaccess
时,不要使用301重定向。使用302直到完成测试,因为浏览器将缓存301s。看

更新:我有点搞错了,
匹配除换行符以外的所有字符,所以包括空格。而且

资料来源:


要从URL中删除.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]
#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]

注意:如果要删除任何其他扩展名,例如删除.php扩展名,只需在上面的代码中将html替换为php

要从URL中删除.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]
#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]

感谢使用Firebase主机的用户,本页没有任何答案。因为您不能在Firebase主机中使用
.htaccess
。您必须配置firebase.json文件。只需在文件中添加行
“cleanUrls”:true
,然后保存它。就这样

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

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

使用
.htaccess
重写静态HTML的URL通常不仅没有必要,而且对网站的性能也有害。启用
.htaccess
也是一个不必要的安全漏洞-关闭它可以消除大量潜在问题。每个
.htaccess
文件的相同规则可以放在该目录的
部分,如果您随后设置
AllowOverride None
,则性能会更好,因为它不需要检查每个目录中的
.htaccess
文件,而且更安全,因为没有root访问权限,攻击者无法更改vhost配置

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

您只需将单个文件从如下结构中移动:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
index.html
about.html
products.html
terms.html
index.html
about/index.html
products/index.html
terms/index.html
对于这样的结构:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
index.html
about.html
products.html
terms.html
index.html
about/index.html
products/index.html
terms/index.html
然后,您的web服务器将呈现相应的页面-如果您加载
/about/
,它会将其视为
/about/index.html


但是,如果有人访问旧网站,这不会重写URL,因此如果它追溯应用于现有网站,则需要重定向才能到位。

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

因此,让我们从重新表述问题和分解任务开始。我们要做到两件事:

  • 如果
    .html
    是请求的URL的一部分(例如
    /page.html
    ),则可明显删除该
    .html
  • 将裁剪后的URL(例如
    /page
    )指向实际文件(
    /page.html
  • 做这两件事都不难。(我们可以通过简单地启用来实现第二个功能。)这里的挑战是在不创建无限循环的情况下同时实现这两个功能

    戴夫自己的回答完成了任务,但它相当复杂,根本不便于携带。(对不起,戴夫。)乌卡斯·哈布尔兹克似乎已经澄清了安莫尔的答案,最后阿米特·维尔玛在这两个问题上都有所改进。然而,他们都没有解释他们的解决方案如何解决如何避免无限循环的根本问题。据我所知,它们之所以能工作,是因为
    _REQUEST
    变量保存来自浏览器的原始请求。因此,条件(
    RewriteCond%{the_REQUEST}
    )只触发一次。因为它不会在重写时触发,所以可以避免无限循环场景。但接下来您要处理完整的HTTP请求-
    GET
    HTTP
    ,所有这些都部分解释了本页中一些更丑陋的正则表达式示例

    我将提供另外一种方法,我认为