I really struggle with .htaccess rules. I really don't understand how it works.. what is wrong with my code. I am testing multiple variations randomly without being able to understand where my issue is coming from.
I would like to have the following rules:
All http/www/non-www pointing to the same https://www URL:
- http://, https://, http:// www -> https://www.example.com (+keep all URL parameters e.g. Google UTM tracking)
Other https subdomains:
- blog.example.com (http or https) -> refers to: https://www.example.com/blog
- same for 'static' subdomain
URL rewriting for key pages of my domain:
- /overview (http or https, www or non-www) -> refers to https://www.example.com/page.php?id=overview)
My website allows users to create their page hosted under xxx.example.com
- http://xxx.example.com (not 'www' nor 'static' nor 'blog') -> keep as is (do not use 'https', as I don't have a wildcard SSL certificate)
There might be some conflicts with "subdomains" created through my Hosting service (BlueHost)
Subdomains - Root Domain - Document Root - Redirection
(star) - .example.com - /public_html/event - not redirected
blog - .example.com - /public_html/blog - not redirected -
static - .example.com - /public_html/static - not redirected
I also have a CNAME configuration..:
Host Record - Points to - TTL
www - example.com - 14400
My current .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule (.*) https://www.example.com [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(static|blog)\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} =/overview
RewriteRule ^ /page.php?id=overview [L]
RewriteCond %{REQUEST_URI} =/faq
RewriteRule ^ /page.php?id=faq [L]
(few more pages like that..)
There are multiple issues:
http:// blog.example.com does not redirect to https version (but weirdly.. 'http:// static..' does redirect to https)
http:// www.example.com/?param=123: '?param=123' is 'skipped'. I have tried adding 'QSA' flag... but does not work either.
(apologies, could not provide all http/https/www/non-www use cases - StackOverflow believes it is a spam)
I don't really know where to start to fix this... 1,000 thanks for your help!
Thanks Damien.