首页 > 解决方案 > 重定向到 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所有内部子链接都可以正常工作。

标签: apache.htaccesswebredirecthttps

解决方案


这是解决方案

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 之后,现在它就可以工作了。


推荐阅读