non www to www with https sub domain issues - apache

I'm using below rules but having issues with sub domains as http://sub.example.com is redirecting to https://sub.example.com/sub instead of https://sub.example.com
RewriteEngine On
# Redirect non-www to (ssl) www
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# Redirect non-SSL to SSL
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
What might be the issue?

I was able to solve the issue by:
RewriteEngine On
# Redirect non-www to (ssl) www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect non-SSL to SSL
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

Rewrite HTTPS domain to another HTTPS domain - htaccess

Currently I have
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^domain2\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
Which redirects www and non-www domain.com to https://domain.com
And redirects www and non-www domain2.com to https://domain.com
domain2 being a parked domain.
But you can still access https://domain2.com and https://www.domain2.com
I want these redirected to https://domain.com
Please advise, thanks
You can combine some of these rules and modify redirect rule for domain2 -> domain to always redirect to https://domain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain2\.com$ [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,NE,L]
It should be as simple as this:
RewriteCond %{REQUEST_SCHEME} !https [NC,OR]
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,NE,L]
Still, keep in mind the last comment from #anubhava about configuring VirtualHost and SSL for domain2.com

Combining Rules in .htaccess file?

I'm wondering how to connect these two rewrite rules in one .htaccess file. Currently, I force https connections to my website with this rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteCond %{HTTP_HOST} !=SERVER_NAME
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Now I want to redirect from my www-subdomain to my real domain. There is a high rated answere here:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I fail to put these two together.
You can use:
RewriteEngine On
# if with www, redirect to https domain without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
If you test first for www and redirect to https, no need to test for http://www.
Or like #PanamaJack said, you just can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,L,R=301]
But you need to change the domain name.

Redirect https to non http and Http to Https in directory with htaccess

I want to make my homepage to redirect to non https if it https
if https://example.com will redirect to http://example.com
and i want to make directory with ssl (just directory using SSL)
if http://example.com/directory wil redirect to https://example.com/directory
I'm use this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
but it just show loop
RewriteEngine On
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} !^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} ^off$
RewriteCond %{REQUEST_URI} ^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

redirecting https://domain.com to https://www.domain.com

I've recently moved my site to HTTPS and im trying to force all http traffic to https and also to the www. version
i.e forcing
https://domain.com
to
https://www.domain.com
I have this in my htaccess and all http to https works, apart from the redirect from https://domain.com to https://www.domain.com
What am i doing wrong?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You need an OR condition here:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
with the above rule https://example.com was not redirecting to https://www.example.com.
For that to happen I have added the following rule too in the apache ssl vhost block
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.exmple.com%{REQUEST_URI} [R=301,L,NE]

Remove www and add https to a domain

I have http://www.example.com and I want it to be https://example.com.
I'm using the following .htaccess rule but it's not redirecting properly from https://www.example.com
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]