从 url 中删除 .html 总是失败

问题描述 投票:0回答:1

可以帮助我,我尝试删除我的 url 上的 .html 扩展名,但仍然不起作用。

这是我的.httaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$  /$1 [L,R=301]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [QSA,L]
</IfModule>
html apache mod-rewrite
1个回答
0
投票

请注意,使用

RewriteRule ^ index.php [QSA,L]
你的规则只是重写
index.php
而不是所有
*.html

如果您只想删除网址末尾的

.html
,您只需要在您的
.htaccess
中使用它:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*) $1.html [L]

Apache 官方文档

但请注意,这不会将用户从

https://example.com/page.html
重定向到
https://example.com/page

例如:您必须将所有锚标签 href 值从

<a href="https://example.com/page.html">Link</a>
替换为
<a href="https://example.com/page">Link</a>

如果你想了解更多关于

.htaccess
以及你可以用它做什么,我推荐你这篇文章。

© www.soinside.com 2019 - 2024. All rights reserved.