complex .htaccess redirect - apache

"domain.com" is my root domain and I have several other domains linked to it as alias domains - let's call those "domain2.com". They work exactly the same as the root but are used for alternate spelling and .net, .de, etc extensions.
I also have some domains that I bought for friends redirecting to their own webspaces - let's call those "domain3.com".
At last I have a single domain "domain4.com" that is also an alias but should behave differently.
I have listed my requirements here:
domain.com
-> root domain
-> wildcard SSL certificate
-> remove www
-> redirect anything to https://
-> "foo" subdomain is automatically redirected to /.sub-foo/ folder
(server setting that I have no influence in)
-> if possible preserve https://foo.domain.com/ in the address bar instead
of https://domain.com/.sub-foo/
domain2.com
-> alias domain linked to domain.com
-> no SSL certificate
-> redirect everything to domain.com, including Request URI and any subdomains
-> address bar should change to domain.com
domain3.com
-> alias domain linked to domain.com
-> redirect everything to external.domain.com, including Request URI
NOT including any subdomains
-> address bar should change to external.domain.com
domain4.com:
-> alias domain linked to domain.com
-> no SSL certificate
-> remove www
-> remove ANY subdomain used
-> remove ANY request URI used
-> ALWAYS redirect to the domain, no matter what
-> redirect that domain to the folder domain.com/subdir/
EDIT: Okay, after a whole lot of try and error I finally have found a mostly complete solution.
### domain3.com ###
RewriteCond %{HTTP_HOST} ^(.*\.)?domain3\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(\.sub-.*\/)?(.*)?$ [NC]
RewriteRule ^(.*)$ http://external.domain.com/%3 [L,R]
### domain4.com ###
# check if any subdomain is used and remove it
RewriteCond %{HTTP_HOST} !^domain4\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)?domain4\.com$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]
# check if redirected subdomain is used and remove it
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(\.sub-.*\/)(.*)?$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]
# check if any request is used and remove it
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(.*)$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]
# redirect to subdir/ directory
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteRule !^subdir/ subdir/ [L,NC]
### domain.com ###
# remove www subdomain and redirect to HTTPS
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
# redirect subdomain to https
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*\.)domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
# redirect http to https
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
### domain2.com ###
# remove www subdomain and redirect to HTTPS root domain
RewriteCond %{HTTP_HOST} !^domain2\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
# redirect subdomain to https root domain
RewriteCond %{HTTP_HOST} !^domain2\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)domain2\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]
### copy domain2.com code for every other "domain2.com" like domain
### I have this code 6 times in my htaccess
For those who wonder about the 2nd RewriteRule part of the domain2.com - I can't use "https://%1domain.com/$1" which would be the "correct" approach, because my server alters the subdomain to "/.sub-foo/" BEFORE the .htaccess is processed.
The only thing NOT working (and I'm not sure I even will get this to work anyways) is that the address bar displays https://domain.com/.sub-foo/bar.php instead of https://foo.domain.com/bar.php
I'll try and talk to my hoster if he can deactivate the automatic redirect so that I can solve this via .htaccess alone.
If anyone has an idea how to shorten this monster - help would be appreciated.

Related

Force www. even with subdomain not working without /

I have a site example.com and also a subdomain and I want the following redirect:
subdomain.example.com -> www.subdomain.example.com
example.com -> www.example.com
I use the following code
RewriteEngine On
# Force www. always and SSL
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
# Force SSL if already www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
Now the problem is that subdomain.example.com doesn't redirect but subdomain.example.com/ does.
I also get a Server Error when going to example.com, although it does redirect to www. correctly.
The point with these redirects is so that I can in the same .htaccess file in the root folder hopefully redirect the subdomains to subdomain.com more easily, I have several domains on one server.
I think you can combine the directives together like this:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
=> Check if HTTPS is off, or if the domain name does not begin with www, and then redirect to https://www.whatever.

Want to redirect TLD domain to a subdomain on https with htaccess. Tried htaccess RewriteRule Redirect found but not working

I want to redirect via .htaccess directives all these:
http://domain.com AND www.domain.com AND http://subdomain.domain.com
TO :
https://subdomain.domain.com
Please note that "subdomain" is an unique subdomain, I do not want to redirect any subdomain but only one.
I'm not familiar with .htaccess directives so I tried all RewriteRule or Redirect snippet found here or elsewere, but it's not working. I must miss something.
EDIT: I added a .htaccess into sur directory of subdomain with this and it works now:
It's OK, I added this in htaccess into the subdirectory of subdomain :
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Try with:
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]

Htaccess Domain Redirects, How Do I Persist Current Page?

I have numerous domains, the main domain is example.com, and the addon domains are example1.com, example2.com, example3.com.
I would like these domains to all redirect to the main domain, example.com.
I can do this with the following code:
RewriteCond %{HTTP_HOST} ^example1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example1.com$
RewriteRule ^/?$ "http://example.com" [R=301,L]
This would redirect example1.com to example.com, simple.
However, if I type in example1.com/products, the URL does not change, no redirection occurs.
How can I make these addon domains redirect to the main domain and keep the users current location (web directory)?
Clear your browser cache and try :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [NC,L,R]
RewriteCond %{HTTP_HOST} example1.com [OR]
RewriteCond %{HTTP_HOST} www.example1.com
RewriteRule (.*) "http://example.com/$1" [R=301,L]

Avoiding multiple Apache redirects for multiple rules

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.

Apache SSL Rewrite with Wildcard Subdomains

I'm attempting to setup an SSL redirect in Apache using RewriteEngine that will do the following:
Redirect traffic to either http://mydomain.com or http://www.mydomain.com to use HTTPS
Redirect traffic to any other subdomain https://*.mydomain.com to use HTTP instead of HTTPS
My reasoning for this is that I'm developing a project that's using a free SSL certificate until launch. This certificate covers the base domain, but none of the wildcard subdomains, and it's a pain to need to bypass the warning every time I visit one of the subdomains.
Edit:
I believe I'm close here, but I still can't get the HTTPS to HTTP redirect to work properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Figured it out. By moving these rules from the sites-available/default file to an .htaccess inside of the website root, I was able to get this working properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]