apache 从url中删除.html始终失败

n53p2ov0  于 2023-04-21  发布在  Apache
关注(0)|答案(1)|浏览(113)

我试图删除.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>
mklgxw1f

mklgxw1f1#

注意,对于RewriteRule ^ index.php [QSA,L],您的规则只重写index.php,而不是所有*.html
如果你只是想删除url末尾的.html,你只需要在你的.htaccess中这样做:

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

Official Apache Documentation
但请注意,这不会将用户从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以及你可以用它做什么,我推荐你this文章。

相关问题