apache2 handle subdomains and SSL - apache

I have this line in my mycooldomain.com.conf (apache2)
Redirect permanent / https://mycooldomain.com
it redirects to https.
What I am trying to do is redirect subdomains to http while all domain requests go through https
for example:
mycooldomain.com/help will redirect to https://mycooldomain.com
while user.mycooldomain.com/blah will redirect to http://user.mycooldomain.com
right now subdomains are being sent to https as well

Add a rewrite rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} user.mycooldomain.com
RewriteRule ^(.*)$ http://user.mycooldomain.com/$1 [QSA, R=301, L]
Redirect permanent / https://mycooldomain.com

Related

How to force HTTPS with amazon lightsail + s3 redirection + route 53

I've created a wordpress blog with Amazon Lightsail and bought several domain names on route 53. My main domain is example.com, I've also bought:
example.eu
example.be
etc.
I'm using s3 buckets to redirect every possible combination (with non www and with www) to www.example.com (in my hosted zone in route 53 it is www.example.com that is redirected to my lightsail static IP).
I've installed a Lets Encrypt SSL certificate for www.example.com
Now I would like to force HTTPS so that all requests redirect to https://www.example.com (usin 301 redirect) which I'm able to do using my s3 buckets but I'm not able to redirect www.example.com to https://www.example.com as it's associated to my static IP address.
What should I do to allow access to my blog only through HTTPS? I've searched for quite some time on the net and found some tutorials saying that I should use cloudfront to handle redirection to HTTPS but they never really apply to my situation.
So I've finally figured it out thanks to this post. I've modified the httpd-app.conf file to add an SSL redirection rule:
RewriteEngine On
RewriteBase /
#SSL redirection
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite index.php
RewriteRule ^index\.php$ - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This rule allows to redirect all HTTP calls to HTTPS using the SSL certificate I installed on my server (for the domain www.example.com).

.htaccess redirect folder to its subdomain causes redirect loop

I have a subdomain sub.domain.com
And the same contents are available on domain.com/sub
But now I want to redirect domain.com/sub to sub.domain.com
If I do that with
Redirect 301 /sub http://sub.domain.com
it causes a redirect loop.
How can this be solved technically? Users should not be able to access the subdomain content under domain.com/sub but should be redirected.
Better to use mod_rewrite rules here with conditions to restrict the rule only for domain.com requests:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^sub(/.*)?$ http://sub.domain.com/$1 [L,NC,R=302]
Make sure to test this after clearing browser cache.

force https on login

I'm trying to force https on the login page with 3.0.13-PL1. I set Server protocol to: https:// but that only changes http:// to https:// affects after the login. I do not want to use mod_rewrite on my server.
Apache
If you are admin Redirect Request to SSL
But I think you aren't:
Apache Redirect HTTP to HTTPS using mod_rewrite
.htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Redirect all HTTP requests (non-www & www, subdomains) to HTTPS

I want to be able to redirect all visitors to a website & its subdomains to HTTPS like the following:
http://example.com -> https://www.example.com
http://www.example.com -> https://www.example.com
http://sub.example.com -> https://sub.example.com
http://www.sub.example.com -> https://sub.example.com
https://www.sub.example.com -> https://sub.example.com
Other redirects:
w (or ww, wwww, etc).example.com -> https://www.example.com
w (or ww, wwww, etc).sub.example.com -> https://sub.example.com
The redirects should be a cacheable HTTP 301 redirect. The configuration used is Apache 2.4.10 + mod-spdy, with Strict-Transport-Security (Plus a wildcard SSL cert).
Currently use the following in .htaccess:
RewriteCond %{SERVER_PORT} !^443
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ "https://www.example.com%{REQUEST_URI}" [R=301,L]
RewriteCond %{SERVER_PORT} !^443
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule ^ "https://sub.example.com%{REQUEST_URI}" [R=301,L]
I've been using spdycheck.org to test this, it works fine for the main domain, with the subdomains I get the following message:
HTTP Traffic Allowed
This website can still be access via HTTP. HTTP
does not provide any widely implemented mechanism for using other
protocols like SPDY. Only traffic over SSL/TLS using the NPN Extension
can be optimized with SPDY. By allowing HTTP traffic, this website is
not even offering SPDY capable browsers the choice of using SPDY. If
possible, this website should redirect all http:// to https://,
ideally with a cachable HTTP 301 redirect.
What's a good way to accomplish this? I could modify httpd.conf if required also.
If you're doing it in your httpd.conf, put in something like:
<VirtualHost _default_:80>
RewriteEngine On
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NE,L,R=301]
</VirtualHost>
After all of your virtual hosts, and ensure that your other virtual hosts are port 443 only. This will make this redirect the global http vhost that will redirect each page individually to HTTPS and give a 301 redirect.

How to redirect a page both to https and to the 'www' version of the site

I recently installed my SSL certificate, and I'm attempting to enforce a https connection to all my pages. However, previously I also redirected all requests to the www version of the request page. When combining an http redirect to https and concurrently redirecting traffic to www, I get a looping redirect warning on browsers. Hence, how can I make .htcaccess rule (I actually just use the directory config file) that will achieve what i want: always https://'www'
Here's the current combination that I have:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}$1 [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) https://www.mydomain.com$1 [R=301,L]
Your question is a duplicate of htaccess redirect to https://www
Also, you can solve the WWW problem with DNS by simply pointing your naked domain to WWWizer's free naked domain redirect service's IP 174.129.25.170