【问题标题】:.htaccess still showing .html extension.htaccess 仍然显示 .html 扩展名
【发布时间】:2021-03-21 09:29:06
【问题描述】:

我正在使用 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

【问题讨论】:

  • mod_rewrite 不会为您更改 HTML 代码,您需要自己更改其中包含的 URL。

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


【解决方案1】:

请根据您显示的示例尝试以下操作。试图在这里修复 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

公平的警告我还没有测试它应该可以工作。

【讨论】:

  • 我想你这里有两次代码# BEGIN WordPress
  • @anubhava,谢谢先生,我从 OP 的问题中复制/粘贴了 :) 现在已修复,谢谢。
  • 我现在是否需要从 html 文件中的 url 链接中删除 .html,例如example.com/test">Test</a>. ?
  • @AbsZaa,对不起,我没听明白,请您解释一下。
  • 我用你的代码替换了 htaccess。 home.html 包含指向 test.html 的链接 example.com/test.html">Test</a></p> 如果我单击该链接,搜索栏会显示:example.com/test.html 我希望它显示:example.com/test 我是否需要删除 .html 扩展名来自 href 网址:example.com/test">Test</a></p>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 2012-01-18
  • 2016-12-04
  • 2016-12-28
  • 2014-03-04
相关资源
最近更新 更多