0

How correctly to rewrite domain to a subfolder with HTTPS ?

This will just redirect all request to subfolder1

RewriteCond %{HTTP_HOST} ^(www.)?example1.com$   
RewriteRule !^subfolder1/ subfolder1%{REQUEST_URI} [L]

If I change to

RewriteCond %{HTTP_HOST} ^(www.)?example1.com$   
RewriteRule !^subfolder1/ https://subfolder1%{REQUEST_URI} [L]

then it will generate a loop

The problem is if I navigate to this url: example.com , then I want it to redirect to HTTPS, and to not change the url to something like: example.com/subfolder1

I want to make the above to work with HTTPS.

Aziz
  • 101
  • 2
  • Your first code snippet appears to do what you are asking? (It is a rewrite, as you requested, not a "redirect" as you state before the directive.) Or is there more detail to your question? Precisely what URLs are you wanting to rewrite from/to? Your second code snippet is unlikely to do anything (as it won't even resolve). – MrWhite Jul 22 '19 at 11:08
  • @MrWhite The problem is if I navigate to this url: example.com , then I want it to redirect to HTTPS, and to not change the url to something like: example.com/subfolder1 – Aziz Jul 22 '19 at 11:23
  • Hhmm, I'm not sure that I follow? Redirecting to HTTPS and rewriting to `/subfolder1` are two entirely separate processes - you cannot combine the two. (You can't redirect to HTTPS _without_ changing the URL - that simply makes no sense.) Do you have other directives in your `.htaccess` file that are perhaps conflicting? – MrWhite Jul 22 '19 at 11:32

1 Answers1

0

I post my solution in case if someone need it. But if there is a better way then let me know.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule (.*)$ /folder/$1
Aziz
  • 101
  • 2