.htaccess文件从网址中删除.html无法正常工作 [英] .htaccess file to remove .html from url not working

查看:134
本文介绍了.htaccess文件从网址中删除.html无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图摆脱页面url中的.html,我发现.htaccess文件中的代码是这样做的唯一方法,并且我几乎在线尝试了所有方法和代码,但是

I'm trying to get rid of the .html in the url of pages, and I found that code in the .htaccess file is the only way doing so and I've tried nearly every method and code online but it just wouldn't work.

制作一个.htaccess文件并将其放在public_html文件中。我正在使用的通用代码是:

Made an .htaccess file and put it in the public_html file. The general code I'm using is:


上的RewriteEngine RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^([[。] + )$ $ 1.html [NC,L]

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

但是我尝试了很多其他代码变体,所以我认为问题不在于代码,而是

But I've tried many many many other variations of the code so I think the issue isn't with the code but the way the file is getting loaded in to the website?

是的,我所有的href都被缩短了,没有.html:ex / < li>< a href = / contact> Contact< / a>< / li>

And yes, all my hrefs are shortened without the .html: ex/ <li><a href="/contact">Contact</a></li>

主要错误是我总是会收到404错误-尝试转到某个页面时找不到此错误,例如ex / website.com/contact。但是website.com/contact.html可以正常工作或无法获得/错误

The main error is in that I always get a 404 error - not found for when I try to go to a page, ex/ website.com/contact. But website.com/contact.html works fine or cannot get / error

我已经在托管网站上进行了尝试,并通过vscode live服务器进行了测试,我觉得.htaccess文件不存在问题吗?由于此解决方案适用于除我以外的几乎所有人。

I've tried this on both my hosted website and testing through vscode live server and I feel like it's an issue not with the .htaccess file somehow? Since this solution works for nearly everyone but me.

推荐答案

#remove html file extension-e.g. https://example.com/file.html will become https://example.com/file
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

对我有用,来自 plothost ,您似乎缺少一行。

is what worked for me, from plothost , you seem to be missing a line.

这篇关于.htaccess文件从网址中删除.html无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆