Redirect only HTTP subdomain to HTTPS subdomain in htaccess - apache

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]

Related

Subdomain to folder in Plesk

I use Plesk and I need to display the content of a folder in a subdomain.
I have the .htaccess file as follows:
RewriteEngine On
#Redirect HTTP and WWW to HTTPS without WWW
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
#Redirect subdomain to folder
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteRule ^(.*)$ /sub/$1 [L,NC,QSA]
The first rules work correctly redirecting to the secure version with www. But the following rules don't work.
In the DNS records, I have created a CNAME record from sub.domain.com to domain.com, is this correct?
When accessing sub.domain.com from the browser, it redirects me to the Plesk login page (sub.domain.com/login_up.php), and any other files (existing in the folder) give a 404 error.
Thanks in advance for the help, I'm a web developer but I don't understand servers 😕
The solution is simple.
You have to create a subdomain of the main domain in Plesk and, in the hosting options, point to the folder as follows:
🏠/httpdocs/folder

.htaccess redirect rules to www and htpps but to exclude subdomain

After reading all relevant answers here regarding .htaccess and redirects, and some experimentation with .htaccess rewrite conditions and rules my problem persists.
I managed to force www and https for my Magento site. Here is what I have at the moment:
RewriteCond %{HTTPS} off
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
After creating the subdomain test.example.com and a test environment at public_html/test/ I want to exclude it from the above rules since the subdomain will neither have a www of a https.
I tried to put this exception in the rules above but with no success.
RewriteCond %{HTTP_HOST} !^test\.example\.com$
For example, when I type http://test.example.com/admin/ to enter the Magento admin, it redirects me to https://www.example.com/admin/ Do I have to also edit the public_html/test/.htaccess file ?
Thank you in advance
Yes, this is happening because of your Rules, the 2nd rule checks if the host value doesn't start with www the redirect the host to www.example.com this rule also redirects any non-www http host to the main domain with www.
To fix this, you can use a single rule to redirect http to https:www excluding the subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} !^test\.example\.com$
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

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 redirect https://domain1.com to https://domain2.com

We have domain.com and domain.com.au pointing to the same website and currently have the following in our .htaccess
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com.au/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.domain\.com\.au$ [NC]
RewriteRule ^(.*)$ https://www.domain.com.au/$1 [R=301,L]
This is semi working to redirect all requests for the site to the correct secured url.
The only issue is when we go to https://domain.com we are always warned by the browser first that the page may be insecure - if we accept the warning then the page correctly redirects to https://www.domain.com.au.
Is there some way to prevent this warning prior to the redirection of the correct secured domain or should we have the .htaccess redirection written differently?

.htaccess redirect from subdomain not passing subdomain part

I have an .htaccess file which catches all subdomains (I am using a wildcard DNS record) and redirects them to a secure login page, but only if the subdomain isn't www. My code for this is:
# Turn rewriting on
RewriteEngine On
# If no subdomain is supplied then add www by default
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
# If the subdomain isn't www then redirect to the login page
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://secure.mydomain.com/a/%1 [R=301,L]
This is working in part. The catching of the www and inserting if it's not there is fine, but what it's not doing is if I type:
http://sample.mydomain.com
I want:
https://secure.mydomain.com/a/sample
but the subdomain bit isn't being appended to the end. I thought the %1 bit would do this but it seems to not be working.
Also, as a second thought, how can I catch:
https://secure.mydomain.com
where nothing is after .com and redirect it back to:
http://www.mydomain.com
Anybody any thoughts?
Do something like that:
# If the subdomain isn't www then redirect to the login page
RewriteCond "%{HTTP_HOST}" !^www.* [NC]
RewriteCond "%{HTTP_HOST}" ^([^\.]+).*$
RewriteRule ^(.*)$ https://secure.mydomain.com/a/%1 [R=301,L]
In general you have to catch the subdomain part and backreference it. So I added a second RewriteCond that does just that. Backreferences in RewriteConds are accessed with '%n' patterns in RewriteRules.