html - .htaccess 仍显示 .html 扩展名

标签 html apache .htaccess mod-rewrite url-rewriting

我正在使用cpanel。 在 public_html 文件夹中我有 2 个文件: 首页.html 测试.html

home.html 链接到 test.html:

<a href="https://example.com/test.html">Test</a>

我不想在浏览器中显示 .html 扩展名。 我的htaccess文件如下,但是.html扩展名仍然显示:

# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^education\.mekan\-turk\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

最佳答案

您能否根据您显示的示例尝试执行以下操作。试图在这里修复OP的尝试。我假设OP想要点击像http://localhost:80/test这样的URL,它应该由http://localhost:80/test.html在这里提供服务.

修复是,当您检查条件 RewriteCond %{REQUEST_FILENAME}.html -f 时,在重写规则时您无需提及 .html 否则它会显示它在浏览器中。另外,将最后 2 条规则保留在顶部也会有所帮助。

# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ http://%{HTTP_HOST}/%{REQUEST_URI} [L]

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^education\.mekan\-turk\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

公平警告,我还没有测试过它,我相信它应该可以工作。

关于html - .htaccess 仍显示 .html 扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65226380/

相关文章:

javascript - Jquery 如何在按钮单击后删除并添加一个类,并在其他函数之前延迟时间

html - 使图像完全填满单元格

linux - Apache2 ErrorLog 意外行为

.htaccess - .htaccess 中的 RewriteRule 在 app.yaml 文件中转换成什么?

php - js 从样式化的 url 获取查询字符串

jquery - 如何隐藏一张 table 并以漂亮的动画方式显示另一张 table ?

php - 使用 PHP 的动态网站

django - 为 Django 静态文件提供服务时的 Apache 403

php - htaccess 从 index.php 重定向到/仅在特定文件夹中

apache - htaccess 重复重命名 php 文件