Configuring .htaccess for multiple domains and SSL - .htaccess

I have a primary domain "example.co.uk" and 6 other domains + 1 IP address pointed to the same folder root.
I would like to use HTTPS for the primary domain i.e. https://www.example.co.uk and redirect all the other domains to the homepage of this domain (avoiding SEO issues and content duplication with the example.com domain and IP address)
How do I achieve this using my current .htaccess file?
RewriteEngine on
#Redirect IP address to example.co.uk.
RewriteCond %{HTTP_HOST} ^12\.34\.567\.890
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]
# Redirect example.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example2.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example3.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example4.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example4\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example5.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example5\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example6.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example6\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]

You can reduce the number of rules by negating the condition check against the HTTP HOST. Then, you'll just need the rule to check HTTPS:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
# redirect all the other domains to the homepage
RewriteCond %{HTTP_HOST} !^www\.example.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/ [L,R=301]
In your question, you said you wanted to redirect all the other domains to the homepage of this domain, but your rules aren't doing that at all. Your rules are redirecting the request to the same request but just this domain. If you want to keep that logic, then you need to add a $1 to the end of the https://www.example.co.uk/.

Related

Redirect HTTP to HTTPS excluding one sup domain and add www if needed

I want to redirect my SSL site (domain.com) with HTTPS protocol but not domain.org which is actually a sub-directory of domain.com (domain.com/domainorg/). I want this:
http://domain.com TO https://www.domain.com
http://domain.org TO http://www.domain.org
I tested my htacess on http://htaccess.mwl.be/ and it works fine but when I actually run it, I get “The page isn't redirecting properly” and it appears to be in a loop when I test all options.
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www.)?domain.org$
RewriteRule ^/?(.*)$ http://www.domain.org/$1 [L,R=301]
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R=301]
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
I am not proficient at htacess at all. What do I need to correct or change?
Why are some of your rules outside <IfModule mod_rewrite.c>? I'd put all of them inside it.
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirecting domain.com http URLs to https (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.com non-www domain.com URLs to www (any non-https will be rewritten to https as well)
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.org non-www URLs to www (any https will be rewritten to http as well)
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
# Redirecting domain.org https URLs to http (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
</IfModule>
You may also force URLs like this domain.com/domainorg/* to be redirected to http://www.domain.org/* using:
RewriteCond RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond RewriteCond %{REQUEST_URI} ^\/?domainorg(\/.*)?$ [NC]
RewriteRule ^.*$ http://www.domain.org%1 [R=301,L]

Magento multistore redirect just one domain.com including path to https

I have a multistore setup on magento with multiple domains.
But I want just one specific store/domain to have https, and redirect all non https urls for that domain to https. Including the whole path.
For example all urls in this list to https://www.
Source urls:
http:// webwinkel.nl/willekeurige-categorienaam
www. webwinkel.nl/willekeurige-categorienaam
http:// www.webwinkel.nl/willekeurige-categorienaam
https:// webwinkel.nl/willekeurige-categorienaam
Target url:
https:// www.webwinkel.nl/willekeurige-categorienaam
I use this for a single store, and in that case it works perfect.
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But for a multistore this doesn't work, because it will redirect every store domain to https, but I want https only for one specific store.
Edit
#itoctopus: Thanx for your reply!
This works for www.webwinkel.nl.
But not for the other domains on the same multistore.
For example I have www.webwinkel.nl, www.webwinkel2.nl and www.webwinkel3.nl.
With your code, they all will redirect to www.webwinkel.nl.
This is my whole htaccess now:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)webwinkel.nl [NC]
RewriteRule . - [E=MAGE_RUN_TYPE:website]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel.nl [NC]
RewriteRule . - [E=MAGE_RUN_CODE:webwinkel]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel2.nl [NC]
RewriteRule . - [E=MAGE_RUN_TYPE:website]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel2.nl [NC]
RewriteRule . - [E=MAGE_RUN_CODE:webwinkel2]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel3.nl [NC]
RewriteRule . - [E=MAGE_RUN_TYPE:website]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel3.nl [NC]
RewriteRule . - [E=MAGE_RUN_CODE:webwinkel3]
# First condition - redirect non-www to www for all domains
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Second condition - redirect HTTP to HTTPS for a particular domain
RewriteCond %{HTTP_HOST} ^webwinkel\.nl$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.webwinkel.nl/$1 [R=301,L]
Add the following code to your .htaccess file to ensure that only a particular domain gets redirected to https://www. The first condition is to handle redirection from non-www to www for all other domains. The second condition is for redirecting your domain to https.
RewriteEngine On
# First condition - redirect non-www to www for all domains
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Second condition - redirect HTTP to HTTPS for a particular domain
RewriteCond %{HTTP_HOST} ^webwinkel\.nl$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.webwinkel.nl/$1 [R=301,L]

.htaccess redirect secondary domain to primary domain with SSL

I am wanting to redirect my secondary domain to my .com primary domain, such that when I visit domain.asia or www.domain.asia it redirects the URL to https://www.domain.com. The problem I am having is that the redirect goes to https://www.domain.asia (not the .com) and the .asia domain does not have a security certificate and because of this shows a warning.
How can I visit domain.asia and have it redirected to https://www.domain.com?
The secondary domain is parked on the same server as the primary domain. Here is my .htaccess file:
# redirect to .com
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.asia$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# redirect to https if not
RewriteCond %{HTTPS} !=on
rewritecond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You have to use:
# redirect to .com
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [OR,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.asia$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
with OR, Use this to combine rule conditions with a local OR instead of the implicit AND.

.htaccess file for multple control pages and wildcard subdomains

I can't seem to get my head around configuring an .htaccess file that sends to different control pages based on the subdomain, then uses wildcards for user subdomains.
What I need is essentially this:
If domain.com, redirect to www.domain.com/index.php
If www.domain.com, rewrite to www.domain.com/index.php
If img.domain.com, rewrite to www.domain.com/img/
If api.domain.com, rewrite to www.domain.com/api/index.php
If admin.domain.com, rewrite to www.domain.com/admin/index.php
If (anything else).domain.com, rewrite to www.domain.com/users/index.php - also sending the value of the subdomain as a variable.
All of the tutorials I've seen send all requests to a single page which is then routed, I would like the htaccess file to do some of the work beforehand.
If your subdomains are not pointing to same documentroot's main domain (you will have to enable mod_proxy)
RewriteEngine on
# 1. www-rule: domain.com to www.domain.com
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# 2. skip www.domain.com
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule . - [L]
# 3/4/5. manage img, api and admin subdomains
RewriteCond %{HTTP_HOST} ^(img|api|admin)\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/%1/$1 [P]
# 6. manage users
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^(.*)$ http://www.domain.com/users/index.php?user=%1 [P]
If your subdomains are pointing to same documentroot's main domain (mod_proxy is not needed here)
RewriteEngine on
# 1. www-rule: domain.com to www.domain.com
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# 2. skip www.domain.com
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule . - [L]
# 3/4/5. manage img, api and admin subdomains
RewriteCond %{HTTP_HOST} ^(img|api|admin)\. [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
# 6. manage users
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^(.*)$ /users/index.php?user=%1 [L]
EDIT:
RewriteEngine on
# 1. www-rule: redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# 2. www.domain.com rewritten to index.php
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule . /index.php [L]
# 3. img.domain.com rewritten to img folder
RewriteCond %{HTTP_HOST} ^img\. [NC]
RewriteRule ^(.*)$ /img/$1 [L]
# 4/5. api or admin subdomains rewritten to /api/index.php or /admin/index.php
RewriteCond %{HTTP_HOST} ^(api|admin)\. [NC]
RewriteRule . /%1/index.php [L]
# 6. manage users
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$
RewriteRule . /users/index.php?user=%1 [L]

Problem with subdomains using .htaccess to redirect non-www URLs to www (301)

I redirected non-www request to www through .htaccess Rewrite Rule.
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) www.%{HTTP_HOST}/$1 [L,R=301]
But now I am having problems with subdomains. When I am accessing touch.111.com then the above rule redirects to touch.www.111.com (which is not accessible), and the website breaks on touch devices.
Please advise me on how to fix the above problem.
You must be specific if you want to redirect only domain.com to www.domain.com and retain sub-domains (such as touch.domain.com) :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301]
This is a generic solution that can work for any domain name:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
see http://www.htaccessredirected.com/force-redirecting-non-www-to-www-htaccess.htm
# For sites running on a port other than 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R]
# And for a site running on port 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Resources