<http:// to http://www.> and <https:// to https://www.> - .htaccess

There is not much to say, because I wanna try to explain it easy.
Is there any way to create a .htaccess that does these things:
example.com > http://www. example.com
http:// example.com > http:// www. example.com
https:// example.com > https:// www. example.com
What I mean is like:
if its http, it goes to the http + www
if its https it goes to the https + www
if nothing is in the front of the domain, it goes to the http + www
I've tried to make it work with another code:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
That code ALMOST works, the only thing that is wrong about it, is that when its https:// example.com it redirects to http:// www. example.com

If that doesn't work, you can write a separate rule for https and http:
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.xplayrs.com/$1 [L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.xplayrs.com/$1 [L]

Replace your code with:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Related

how to redirect www and non www to https for specific domain using htaccess

I need to redirect www and non www to https. I have looked everywhere on stackoverflow but can't find quite what I'm looking for.
The rules are:
example.com and www.example.com and https://example.com
must redirect to https://www.example.com
It must match the domain and extension example.com. It cannot be a wild card which also
matches abc.com or 123.com or example.net or anything else
It must match for www subdomains only. It cannot be a wild card which
also matches sub.example.com or thisisademo.example.com
I currently have:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
However if someone enters www.example.com it still goes to the http version instead of https.
What I think I actually need here is RewriteCond regex to match exactly and only "example.com" and "www.example.com"
Thanks
You can use the following rule to redierct non-www or www to https://www in just one redirection
#redirect http non-www to https://www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
#redirect https non-www to www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Clear your browser's cache before testing this rule.
This is the only way it works for me - with !on instead of off and with %{ENV:HTTPS} instead of %{HTTPS}:
#non-www. http to www. https
RewriteCond %{ENV:HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#non-www. https to www. https
RewriteCond %{ENV:HTTPS} on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#First rewrite any request to the wrong domain to use the correct one (i.e. www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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]

Redirect all domains to www with https

I'm trying to redirect all traffic to example.com to use https://www.example.com
This I have this working with :
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
But, the https://example.com still does not redirect and causes a certificate error.
So it should redirect:
http://example.com,https://example.com, http://www.example.com
to https://www.example.com
Any ideas ?
Thanks,
Rick
Both rules can be combined into one like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
Also keep in mind that cert error might still some since rewrite module runs after cert negotiation with browser.
You can use this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Force HTTPS and WWW for domain and only HTTPS for subdomains HTACESS

I've been trying to configure this for my website, but not being able to.
I use to have a cond on my .htaccess to force www for the main domain and nothing for subdomains, but since I got a SSL, I'm having some problems.
It's a wildcard SSL.
What I need is force HTTPS:// WWW on the main domain, and HTTPS:// on subdomains.
I.E: http://www.domain.com -> https://subdomain.domain.com
Is there any rule for that?
Thanks!
EDITED
Now I'm using like Jon posted
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [NC]
RewriteRule ^.*$ https://www.domain.com.br%{REQUEST_URI} [R,L]
# ---------- #
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com\.br$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
The thing is, when on main domain if I type HTTP:// with or without WWW, ir forces HTTPS:// and WWW, that's ok...
But on subdomain, when I type HTTP it doesn't force HTTPS, it redirects to the main domain only... that does not happen if I put a .htaccess inside the dir of the subdomain.
With a .htaccess inside my subdomain dir, if I type HTTP, it forces HTTPS normally...
Any suggestions?
Try:
RewriteEngine On
# for subdomains
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L]

.htaccess: force www on all pages, force https on one page, force http on all other pages

I have this site, example.com. What I want to do is force www on all pages, force https on one page, force http on all other pages.
I am using the following .htaccess code to redirect all http traffic to www:
#Redirect to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
and the following to set SSL on my "form" page:
#Force SSL on a specific directory
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} form
RewriteRule ^(.*)$ https://www.example.com/form/$1 [R,L]
Problem is, when I hit https://example.com/, it does not redirect to www (https://www.example.com), and especially not http and www (http://www.example.com) like I want it to. I tried this:
#Redirect SSL to www
RewriteCond %{ENV:HTTPS} on [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=permanent,L]
but that was a really dumb attempt and didn't work. How can I accomplish all three at once?
Try this:
# Handle nonSSL
RewriteCond %{HTTPS} on
RewriteRule !^/?form(/|$) http://www.ravicti.com%{REQUEST_URI} [L,R=301]
# Handle SSL
RewriteCond %{HTTPS} off
RewriteRule ^/?form(/|$) https://www.ravicti.com%{REQUEST_URI} [L,R=301]
# Add the missing www for both HTTP and HTTPS
RewriteCond %{HTTP_HOST} ^ravicti.com$ [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule (.*) http%2://www.ravicti.com%{REQUEST_URI} [L,R=301]

Resources