2
votes

I’m trying to do that:

Force the https for my main domain.

http or https://www.domain.com -> https://domain.com
http or https://domain.com -> https://domain.com

this is htaccess coding for domain

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

this is working fine

But not for subdomains

I want result as

https://www.domain.com/index.php?username=abc => http://abc.domain.com
http://www.domain.com/index.php?username=abc => http://abc.domain.com

And always removing the www and https to http.

but no idea how to write htaccess code for subdomain

1
this is for my domain htaccess RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] but i have no idea for http subdomain - Amy
This question is already answered here: stackoverflow.com/questions/4398951/… - Joro
@Joro Sorry, no, that question does not answer this question. - arkascha
@Joro this question is different in that he wants to enforce https for his primary domain only, while enforcing http for non-www subdomains. - Ultimater
@Ultimater yes you are right ,i need some help . - Amy

1 Answers

1
votes

This will first check if you need to redirect to a subdomain based on the username parameter in the query string if requesting index.php. Then when rewritting to that subdomain, it will copy the entire querystring along with, but omitting the username parameter from it while preserving all other parameters.

The second part will check for your primary domain with or without www, then enforce https on it, if we didn't already decide to redirect to a subdomain.

RewriteEngine On

#match either the www subdomain or no subdomain
  RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
#which request index.php
  RewriteCond %{REQUEST_URI}  ^/index\.php
#and contain a username paramater
  RewriteCond %{QUERY_STRING} ^(.*?)(?:^|&)username=([^&]+)(.*?)$
#then rewrite them to username.domain.com without the username parameter
  RewriteRule ^ http://%2.domain.com%{REQUEST_URI}?%1%3 [R,QSA,L]

#match either the www subdomain or no subdomain
  RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
#which was requested without https
  RewriteCond %{HTTPS} off
#then redirect to https
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]