Subdomains Display The Wrong Content Using HTTPS - apache

I am using a wildcard SSL on my main domain and a few sub-domains (apps, dashboard, support, training). The problem I am facing is that when I try to access a sub-domain via HTTPS, it shows the index/main page of the root domain, but when the sub-domain is accessed via HTTP, it shows the correct page. I would like to know what information I would need to put in the .htaccess file to make the sub-domains:
Automatically use HTTPS.
Show the correct content on that sub-domain when using HTTPS.
The sub-domains don't currently have an .htaccess file, because I have no idea what should be put in there at this point.
Here is an example of the HTTP sub-domain: http://goo.gl/Mcg66l
Here is an example of the HTTPS sub-domain: http://goo.gl/Aw9yl6
Here is the main domain: http://goo.gl/TfLjuy
The main domain is doing exactly what I want it to do (www to non-www and automatic https redirection), but the sub-domains are not doing what I want them to do, as described above.
Here is the .htaccess file on the main domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
Lastly, I am using cPanel under a reseller account serviced by HostGator.
Thanks for taking a look at this problem!

I had the same exact issue, also using a HostGator account. I found the answer on another question that you can find here.
They go about solving it by applying HTTPS for the main domain, forcing HTTP on a subdomain, and then as a bonus remove all WWW's
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]

Related

Not redirecting properly the https when changing domain

I have two domains loaded in my Plesk websites and domains section,
the two domains are without www,
so let´s say the two domains are:
example.com
example.es
When I redirect from example.es to example.com it is done but with http, not with https.
So, I decided to edit my .htaccess file with the follwing instructions:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
But my page is not loaded properly, because seeing the web inspector it creates an infinite loop of requests to my main page example.com.
Anyone knows some kind of solutions?
And I have Application Load Balancer for the ssl certificate that is inside my EC2 AWS machines.
Try the following instead:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The connection between the load balancer and your application server is likely HTTP only, so the HTTPS Apache server variable is always off.
(Test first with a 302 - temporary - redirect to avoid potential caching issues.)
Although if you are redirecting everything (ie. example.es) to example.com then do something like the following instead:
RewriteCond %{HTTP_HOST} !=example.com [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
Assuming both domains point to the same document root then you don't need your redirect in Plesk, which will otherwise result in 2 redirects by the sound of it.

Redirect only HTTP subdomain to HTTPS subdomain in htaccess

How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would create a redirect loop (if executed at all). In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop).
These directives would need to go in the .htaccess in the document root of your subdomain. Or at the top of your root (WordPress) .htaccess file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.
This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This checks that HTTPS does not contain "on" and the subdomain is being requested before redirecting to HTTPS on the same host.
Although if WP is simply redirecting the main domain, then you could do all this in .htaccess and simply remove the condition that checks against HTTP_HOST. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example:
RewriteCond %{HTTP_HOST} ^((subdomain|www)\.)?example\.com [NC]
Please try one of the followings:
you need to edit
Redirect "/" "https://yourwebsite.com/"
OR
you dont need to edit following
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

htaccess subdomain silent redirect

I have subdomains for each user, user1.domain.com that successfully points to domain.com/user1.
My question is how do I do it so that http://user1.domain.com/admin does a SILENT redirect to http://domain.com/admin?
I want to apply this to all subdirectories in the subdomain, so user1.domain.com/cart silent redirects to domain.com/cart & user1.domain.com/cart/checkout/confirmation -> domain.com/cart/checkout/confirmation so on and so forth. Bear in mind that this directories don't actually exist in the subdomains, only the root domain.
I've tried searching Stackoverflow but could not find an answer to my specific issue. Thanks!
It's not a redirect unless the browser knows about it, there's no such thing as a "silent" redirect. You have 2 options, you can either proxy on behalf of the browser, or you can internally rewrite the request if both user1.domain.com and domain.com share the same document root.
To proxy:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,P]
The P flag indicates a proxy. This rule will not work unless you have mod_proxy loaded. If all your domains point to the same document root, then you can simply rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

Redirecting all subdomains to a domain

I am having an issue with one of my sites where I need to redirect all subdomains to the main domain name. I have a Drupal site set up that shares content between 2 domains (using the Domain module). Here is basically what is going on.
In my .htaccess file I have these rules..
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
This redirects any subdomain to mydomain.com, which is great!
The problem is that I have another domain (myotherdomain.com) which uses the same Drupal site to share content with via the Domain module.
With that .htaccess rule in place when I go to myotherdomain.com it is redirecting to mydomain.com which I do not want to happen. Is there any way that I can stop that from happening?
To recap:
anything.mydomain.com needs to redirect to mydomain.com
any traffic to myotherdomain.com needs to stay at myotherdomain.com and not get redirected.
Any RewriteCond before a RewriteRule will be applied. Have you tried adding simply RewriteCond %{HTTP_HOST} !^myotherdomain\.com$ [NC]?
Just add another exclusion condition for your other domain:
RewriteCond %{HTTP_HOST} !^myotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
This way, any request for "myotherdomain.com" won't get redirect, and any request for "mydomain.com" won't get redirected.

.htaccess for domain change with subdomains - apache + plesk

We're using a plesk based system and just recently changed domain names. I'd like to redirect all requests coming in to the old domain to the new. There are many question asked in a similar fashion but mine is a bit different. I'd like to ensure that all subdomains get routed to the same subdoamin on the new domain. I set up a generic htaccess in the docroot but for some reason it is also applying to all subdomains.
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
How can I make this more general so that subdomains also appropriately get routed? For bonus points, how can I route https requests to https and http to http.
I'd like to add that the rule transforms the first url to the second which is not desirable:
http://SUBDOMAIN.olddomain.com/somepath/somefile.php
http://newdomain.com/subdomains/SUBDOMAIN/httpdocs/somepath/somefile.php
The proper transform should create the following url:
http://SUBDOMAIN.newdomain.com/somepath/somefile.php
Lastly, this should work with wildcard subdomains.
Thanks in advanced!
Try:
# ignore "www."
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://%1.newdomain.com/$1 [L,R=301]
You'd want this before the rule that you already have.
And since you've edited your question, you want an additional internal rewrite for handling subdomains in general on your new site, this has nothing to do with the redirect. This is brand new functionality that is outside of the scope of a 301 redirect. You're going to need special rules to handle internal routing of subdomains:
RewriteCond %{REQUEST_URI} !^/subdomains/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.newdomain.com$ [NC]
RewriteRule ^(.*)$ /subdomains/%1/httpdocs/$1 [L]