Avoiding multiple Apache redirects for multiple rules - apache

I have a need to put some redirects in place to enforce some rules and handle some URL changes.
Specifically as follows:
If the protocol is http, enforce https
If there is no subdomain or the subdomain is not www, enforce www
If the domain is www.domainA.com and the path does not begin with /en/, /fr/, /de/ or /es/, enforce /en/
If the domain is www.domainB.com and the path does not begin with /b_en/, /b_fr/, /b_de/ or /b_es/, enforce /b_en/
I've been trying to get this working so that only one 301 happens at any one time and we don't end up with a chain of 301s. For example a request to http://domainA.com could potentially be redirected 3 times:
http://domainA.com 301 to...
https://domainA.com 301 to...
https://www.domainA.com 301 to...
https://www.domainA.com/en/
However I've not been able to come with a solution.
This would live in a .htaccess file.

You can use in your .htaccess:
# domainA
RewriteCond %{HTTP_HOST} domainA\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(?:en|fr|de|es) [NC]
RewriteRule ^ https://www.domainA.com/en%{REQUEST_URI} [NE,L,R=301]
# domainB
RewriteCond %{HTTP_HOST} domainB\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/b_(?:en|fr|de|es) [NC]
RewriteRule ^ https://www.domainB.com/b_en%{REQUEST_URI} [NE,L,R=301]
# https & www
RewriteCond %{HTTP_HOST} (?:^|\.)(domainA\.com|domainB\.com)$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
Never more than one redirection.

Related

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.

.htaccess - multiple page by page redirects + www & https rules

Been struggling and playing with this and can't get it working in Apache.
I'm not well versed in the syntax and was rather trying to build something based on existing questions on here.
Trying to force everything to HTTPS and www, and also add over 200 individual page redirects.
RewriteEngine on
# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
Redirect 301 /PAGE1 /PAGE2
Thanks a lot in advance!
in thw www part remove the https condition and redirect with https and www anyway
in the https part remove the www condition
after those 2 ... continue with all your paged redirecting
RewriteEngine on
# Redirect to domain with HTTPS.
RewriteCond %{HTTPS} off
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
Redirect 301 /PAGE1 /PAGE2

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.

Best way to redirect to non-www AND enforce SSL at the same time

I created a way to redirect www to non-www and to redirect any http to https. However, I'm wondering if there's a more elegant and efficient way to do this. Here's my code:
# Force SSL
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
# Rewrite all http to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://{HTTP_HOST}.com/$1 [R=301,L]
You can enforce non-www and https using a single RewriteRule :
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
The combination of OR and AND conditions above allows us to manipulate 2 types of requests at one time 1) http www, 2) https www and The rule always redirects them to the ssl non-www version. You can Replace R with R=301 to make the Redirect Permanent.

HTACCESS 301 Redirect Rule to point all URL Variations to the Live URL

I am trying to achieve something which is working 99%, but there is a tiny issue.
Let's say my live URL is https://www.example.com/sample-page/
I want all the following URL variations to redirect to the live URL with a 301 status.
http://example.com/sample-page/
http://www.example.com/sample-page/
https://example.com/sample-page/
All of the above should redirect to https://www.example.com/sample-page/
I managed to get this working by using the htaccess rule displayed below.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The problem with the above rule is this: http://example.com/sample-page/ does a double redirect.
http://eyeacademy.com/expert-eye-examination/
301 Moved Permanently
https://eyeacademy.com/expert-eye-examination/
301 Moved Permanently
https://www.eyeacademy.com/expert-eye-examination/
200 OK
As you can see, http redirects to https and then https non-www redirects to https www. I have been trying a few tweaks to this rule and reading up, but I am sure someone here would have a quicker and more robust solution?
You can use this single rule to redirect http -> https and add www and there is no need to hardcode host name in the rule:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
You can also reorder your existing rules and avoid multiple redirects like this:
# first add www and make sure it is https://
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# http -> https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Use an or flag in your RewriteCond directive. Replace everything with the following:
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]