0

I want any access to my website to redirect to https://www. but I have a RewriteBase.

Suppose my website is example.com, I want:

example.com to redirect to https://www.example.com

http://example.com to redirect to https://www.example.com

https://example.com to redirect to https://www.example.com

https://www.example.com to redirect to https://www.example.com

And my website files are located in a subfolder, so I have a RewriteBase which is for example:

RewriteBase /~myuser/mysite

My .htaccess file contains:

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]

I referred to the following question to write my .htaccess file: htaccess redirect to https://www

As a result, if I write on in a browser a url such as example.com/page1 (which is a valid page), it redirects to https://www.example.com/page1 however, I get the following error:

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.

I think the problem is caused by the RewriteBase, but I do not know how to fix it.

Note that when I access the website main url https://www.example.com all internal sub-links work correctly.

EddyG
  • 2,051
  • 3
  • 22
  • 46

1 Answers1

0

Here is the solution

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

Simply I placed the RewriteBase at the end after the RewriteRule and now it works.

EddyG
  • 2,051
  • 3
  • 22
  • 46