apache - 重定向到 https://www 与 RewriteBase 一起使用时如何修复未找到错误?
问题描述
我希望对我的网站的任何访问都重定向到,https://www.
但我有一个 RewriteBase。
假设我的网站是example.com
,我想要:
example.com
重定向到https://www.example.com
http://example.com
重定向到https://www.example.com
https://example.com
重定向到https://www.example.com
https://www.example.com
重定向到https://www.example.com
我的网站文件位于一个子文件夹中,所以我有一个 RewriteBase,例如:
RewriteBase /~myuser/mysite
我的 .htaccess 文件包含:
RewriteEngine On
RewriteBase /~myuser/mysite
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
我参考了以下问题来编写我的 .htaccess 文件:htaccess redirect to https://www
结果,如果我在浏览器中写入诸如example.com/page1
(这是一个有效页面)之类的 url,它会重定向到https://www.example.com/page1
但是,我收到以下错误:
Not Found
The requested URL /page1 was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
我认为问题是由 引起的RewriteBase
,但我不知道如何解决它。
请注意,当我访问网站主 url 时,https://www.example.com
所有内部子链接都可以正常工作。
解决方案
这是解决方案
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /~myuser/mysite
只需将 RewriteBase 放在 RewriteRule 之后,现在它就可以工作了。
推荐阅读
- r - 使用 gganimate 演示一个实验
- catel - 我无法让 Catel.sln 加载其项目
- excel - 基于变量设置范围值的宏
- java - 仅当建筑物完全在 cameraView 内时才显示 GroundOverlay
- python - 删除重复数据框保留第一个或不为空值
- flutter - Where to add custom colors in ThemeData in Flutter
- python - Python 输出 RETURN 在 Api 密钥短信脚本中不起作用
- grunt-contrib-uglify - grunt-contrib-uglify 到 grunt-terser 迁移
- alignment - 使用 bwa 的未映射读取
- javascript - 为什么我的 codepen.io 代码在我的括号实时预览中不起作用?