【问题标题】:Remove .html at the end of URL whilst rerouteing to HTTPS在重新路由到 HTTPS 时删除 URL 末尾的 .html
【发布时间】:2019-12-25 20:44:19
【问题描述】:

为了使用 SSL 证书,我需要将我的网站从 http 重定向到 https,但我还想删除 URL 末尾的 .html。我似乎无法让它工作。

这是我的代码:

# Redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Remove .html from URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]

以下代码禁用 SSL 并且不会重新路由到 HTTPS。

【问题讨论】:

  • 啊哈,所以我应该换个顺序?
  • 订单也可以。在新的浏览器中测试。

标签: php html .htaccess web


【解决方案1】:

这应该可行:

 # Redirect HTTP to HTTPS
 RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

 # Remove .html from URL
 #RewriteCond %{REQUEST_FILENAME} !-f
 #RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)\.html /$1 [L,R=301,NC]

确保在每次测试之前清除浏览器缓存。

问题出在RewriteCond %{REQUEST_FILENAME} !-f 行,因为该文件确实存在并且未处理重写规则。所以我删除了它(注释)以及下一行RewriteCond %{REQUEST_FILENAME} !-d,如果(.*) 内没有目录并且你的测试正确,则应该留下它。不过,我没有用 RewriteCond 对其进行测试。但我使用了这段代码并且工作正常。

【讨论】:

    【解决方案2】:
    #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]
    

    【讨论】:

      猜你喜欢
      • 2016-04-13
      • 2023-01-18
      • 2020-07-14
      • 2021-08-11
      • 2016-08-29
      • 2016-03-27
      • 2011-02-18
      • 1970-01-01
      • 2019-11-18
      相关资源
      最近更新 更多