htaccess force www not for cdn - apache

I have a problem with .htaccess forcing www.
I have the main site http://www.portalesardegna.com and I want to redirect http://portalesardegna.com to the www version, but I have even a CDN, http://cdn.portalesardegna.com, if I look the site with pingdom http://tools.pingdom.com/fpt/#!/eHy16D/www.portalesardegna.com all the files in the cdn are redirected to the www version.
Some information that can be useful:
The CDN is on an external service, and the dns point to another server, not mine.
I have some images from another subdomain in my site, private3.portalesardegna.com, and all of these images are not redirected.
Here is the code i use in the htaccess
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^cdn\..+$ [NC]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Try this rule as as your very first rule in your root .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(cdn|www)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Related

htaccess isn't redirecting to https

I'm trying to create an htaccess file in the root directory that will also affect subdomain directories (the subdomain works from a directory in the root).
Firstly, will this work if I have one htaccess file in the root? (will it work for subdomains in browsers (as they are actually just directories from root?) ).
The problem that I'm having is that when I try to visit http://demo.example.com, instead of it redirecting to https://demo.example.com it just remains using http://. This also is the case when trying to view http://www.example.com (it doesn't redirect to https).
This is my entire htaccess file as it currently stands. It seems quite bloated to me. Is there a way I can just redirect everything to https, regardless of subdomain / root ?
<IfModule mod_rewrite.c>
RewriteEngine On
# redirect subdomain to https
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
</IfModule>
Can you try these rules in your site root .htaccess:
RewriteEngine On
# add www and https to main domain
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# redirect everything to https
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Test it in a new browser or completely clear your browser cache before testing.

htaccess rewrite for https multiple domains

I'm just going crazy with my issue and hope for your help.
I have one webstore with two domains linking to one same path. And webstore is choosing itself which content should be shown depends on domain.
www.yogabox.de - German content
www.yogabox.co.uk - English content
I'm trying to rewrite all kinds of yogabox.de to https://www.yogabox.de und the same for yogabox.co.uk to https://www.yogabox.co.uk
Here is the result:
I'm using those rules:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.yogabox\.co\.uk$
RewriteCond %{HTTP_HOST} !^yogabox\.co\.uk$
#RewriteCond %{HTTP_HOST} !^www\.yogabox\.de$
RewriteRule ^(.*)$ https://www.yogabox.de/$1 [R=301,L]
RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^yogabox\.co\.uk$
RewriteCond %{HTTP_HOST} !^www\.yogabox\.de$
RewriteCond %{HTTP_HOST} !^yogabox\.de$
RewriteRule ^(.*)$ https://www.yogabox.co.uk/$1 [R=301,L]
Only https://yogabox.de and https://yogabox.co.uk are wrong. Where is the problem?
I have already checked the problem with not valid certificate like here WWW to NON WWW Urls (Remove WWW) using Apache (.htaccess)
But the certificates are valid for www and without www.
The problem is that your rules don't match the ssl non-www urls, so the redirection from https://example.com to https://www.example.com isn't happening on your server. .
You can use the following generic rule to redirect your domains to https://www :
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^.*$ https://www.%1%{REQUEST_URI} [NE,L,R=301]
Make sure to clear your browser cache before testing these rules.

shared htaccess: redirect multiple domain roots

My situtation is that multiple domains share a htaccess file and I need to redirect the root / to each language folder.
So I want ONLY the root / of each domain to be redirected like:
www.my-en.com/ to redirect to /en/
and
www.my-de.de/ to redirect to /de/
Unfortunately I need to accomplish this with htaccess as I CANNOT change any other settings.
So far all solutions I found look like this
redirct 301 /old-path http://www.new-domain.com
which will always be applied to all domains using the htaccess.
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?my-en\.com$ [NC]
RewriteRule ^$ http://www.my-en.com/en/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?my-de\.de$ [NC]
RewriteRule ^$ http://www.my-de.de/de/ [R=301,L]
# Redirect others to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Using multiple rewrite rules .htaccess

I made the switch of my website from http to https. Now, I want to 301 redirect all http content to https.
I want to use this rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The issue is I also have another rule already in place which redirects all non-www pages to www ones.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]
How can I combine it so whatever link users write (http non-www ; http www ; https non-www) all redirect to https://www.example.com
Cheers!
You can use this single rule to add www and http->https in single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
This rule should be kept just below RewriteEngine line and make sure to clear your browser cache when testing this change.

htaccess rewrite rules - domain/subdomain https/www

I have my main domain and a subdomain. Subdomain is located in a subdirectory on the root directory of main domain and this cannot be changed. Main domain has an SSL while subdomain doesn't.
Now I am trying to force https and www for the main domain and force no https and no www for the subdomain. So at the end the main domain disregarding how it will try to be accessed should redirect at https://www.example.com and same time subdomain should be http://sub.example.com
All other possible combinations of accessing them should redirect to the above 2. So far, I am successfull to do this for the most possibilities but I am failing to do so for the case when I access the subdomain with https://sub.example.com. It won't redirect to the http version, rather it will try to load the page through https, browser throws the security warning about the SSL and if I proceed, it load the contents of the main domain site through the subdomain and the insecure warning on the browser.
Following is the htaccess rules I am trying, on the htaccess on the root directory, that affects both main site and subdomain.
How to improve them and achieve the desired outcome?
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\.example\.com [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !sub\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} www\.sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
To redirect only the main domain to https, you can use the following rule :
RewriteEngine on
#https to http (subdomain)
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule ^ http://sub.example.com%{REQUEST_URI} [NE,L,R]
#main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]