htaccess rewrite for https multiple domains - apache

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.

Related

Redirect multiple domains (http https www & non-www) to a new domain using htaccess

I have 3 different domains (with two different TLD) and I would like that all my domains redirect to only one (including all format http https www or non-www) -> https://www.newdomain.com
All my domains :
http://olddomain.net
http://www.olddomain.net
https://olddomain.net
https://www.olddomain.net
http://olddomain2.com
http://www.olddomain2.com
https://olddomain2.com
https://www.olddomain2.com
http://newdomain.com
http://www.newdomain.com
https://newdomain.com
https://www.newdomain.com
So all these domains have to redirect to : https://www.newdomain.com
I have tried many different configurations of my htaccess but I do not handle all the redirections. So far what I found :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.net$ [OR]
RewriteCond %{HTTP_HOST} ^olddomain2\.com$ [OR]
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [L,NE,R=301]
But it does not handle all the variations. I think this is confusing Google bot as my new main url is not indexed.
How can I modify my htaccess so it handle all the possibilities ?
It can be done using single redirect rule like this:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$ [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before checking your URLs. I would have checked condition if your HTTP_HOST is NOT equal to your new domain but then I am afraid that it can touch your other domains too which you don't want to redirect to your new domain.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.net [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain2\.com [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]

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 http to https with www. Without redirecting sub domain

I have a RewriteRule that redirects my main domain to https://www.sta-games.com which works fine, but when I try to access my subdomain http://files.sta-games.com, it redirects to my main domain.
Heres my redirect rules
#HTTPS Redirection
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can anyone see the problem?
You need to adjust your rules so that it looks for the whole domain instead of just part of it. Right now you are only looking for the absence of www. So that is why your subdomain redirects.
First you need to combine your rules because it seems you want to redirect if https is not on and there is no www, so make that one rule. Then use your actual domain name in the rule. Replace example.com with your domain. This should fix your issue.
#HTTPS Redirection
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [L,R=301]
Have an additional skip condition for all domains except the main domain:
RewriteCond %{HTTP_HOST} ^(www\.)?sta-games\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.sta-games.com%{REQUEST_URI} [L,R=301,NE]
Test this after clearing your browser cache.

htaccess wildcard redirect not working

I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]

htaccess force www not for cdn

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]