2

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

arkascha
  • 41,620
  • 7
  • 58
  • 90
Amy
  • 161
  • 1
  • 10
  • 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 Sep 27 '15 at 07:31
  • This question is already answered here: http://stackoverflow.com/questions/4398951/force-ssl-https-using-htaccess-and-mod-rewrite – Joro Sep 27 '15 at 07:41
  • @Joro Sorry, no, that question does _not_ answer this question. – arkascha Sep 27 '15 at 07:44
  • @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 Sep 27 '15 at 07:53
  • @Ultimater yes you are right ,i need some help . – Amy Sep 27 '15 at 08:57

1 Answers1

1

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]
Ultimater
  • 4,647
  • 2
  • 29
  • 43
  • -> thanks dude from the bottom of mah heart but a little problem ,its ok username=abc then abc.domain.com url is working but cannot fetch data using username=abc – Amy Sep 27 '15 at 09:16
  • If you want to include the username parameter, then use: `RewriteRule ^ http://%2.domain.com%{REQUEST_URI} [R,L]` – Ultimater Sep 27 '15 at 09:17
  • i have tried but nothing happened url is working fine but cannot fetch data using username=abc https://domain.com/index.php?username=abc page shows result abc but abc.domain.com shows blank page – Amy Sep 27 '15 at 09:24
  • If you run `http://abc.domain.com/index.php?username=abc` directly, is it able to fetch data? – Ultimater Sep 27 '15 at 09:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90723/discussion-between-yupok-admin-and-ultimater). – Amy Sep 27 '15 at 09:29
  • a little problem arise – Amy Sep 27 '15 at 11:25