.htaccess - 需要帮助为 HTTPS 正确配置 .htaccess
问题描述
我正在尝试正确配置和编写我的 .htaccess 文件。有些事情我已经尝试过了,但不知何故,它们都没有奏效。
我正在尝试完成以下重定向:
- 将所有文件夹(包括子文件夹)从 HTTP 重定向到 HTTPS
- 删除“.html”结尾,使 url 显示 www.domain.com/photos 而不是 www.domain.com/photos.html
- 在主页上删除“index.html”,因此它只显示 www.domain.com
有些事情可行,但是当进入像 www.domain.com/sub 这样的子目录时,页面会从 HTTPS 更改为 HTTP。
此外,当通过 HTTPS 访问页面时,它不会正确加载 HTML 文件中指定的字体 ()
请参阅下面的代码:
<IfModule mod_rewrite.c>
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS. # i.e. http://www.example.com/foo/ to https://www.example.com/foo/ # The leading slash is made optional so that this will work either in httpd.conf or .htaccess context
# remove .html; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .html to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
解决方案
我想通了,它到目前为止工作:
<IfModule mod_rewrite.c>
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
# Redirect to www
RewriteCond %{HTTP_HOST} !^www\.domain\.ch [NC,OR]
# Redirect to https
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://www.domain.ch%{REQUEST_URI} [NE,R=301,L]
# remove .html; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .html to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
推荐阅读
- python-requests - 通过 Python 的 Telegram SendPoll:无法解析选项 JSON
- azure - 尝试使用 Azure/MSAL 通过 Azure 活动目录和 API 身份验证实现 SSO
- linux - 在非 GUI 环境中使用 QPainter
- c# - 如何在 linux 中为 .Net Core HttpListener 配置 ssl
- javascript - 如何验证表单数据,然后通过 AJAX 发送,然后在同一页面上显示提交的结果
- java - java maven导入问题:错误:com.google.gson包不存在
- c - 为什么 CS50 IDE 不让我做基本的数学运算?难道我做错了什么?
- android - 亲属秩序的孩子不工作
- php - 控制器中 Laravel 6 参数的奇怪行为
- ember.js - 没有 cli 的 emberjs(版本 3)