How to properly write a RewriteCond for https and subdomains

74 Views Asked by At

Thus far I've gotten this far with my RewriteConditions but it's from hacking and slashing from internet examples. They seems to behave a little funny at times, like showing the directory in the url, so I assume I'm not doing it right.

The desired behaviour: I have multiple subdomains all pointing to the same root. Based on the subdomain value I want to redirect to a sub-folder in the root. I also want to force HTTPS. I have multiple subdomains, but I'll only show two for the sake of brevity. All the other conditions are virtually identical.

Here's my .htaccess code:`

RewriteEngine On

RewriteCond %{HTTP_HOST} purchase.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !purchase/
RewriteRule (.*) https://purchase.mydomain.com/purchase/$1 [L]

RewriteCond %{HTTP_HOST} booking.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !booking/
RewriteRule (.*) https://booking.mydomain.com/booking/$1 [L]

`

Thank you,

Mike

1

There are 1 best solutions below

1
On BEST ANSWER

Change your rules like this:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(purchase|booking)\.mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP_HOST} =purchase.mydomain.com
RewriteRule ^((?!purchase/).*)$ /purchase/$1 [L]

RewriteCond %{HTTP_HOST} =booking.mydomain.com
RewriteRule ^((?!booking/).*)$ /booking/$1 [L]

http:// or https:// in target will redirect instead of rewriting.