301 Redirects not working properly - .htaccess

I want to redirect every request from http:// to https:// and from www.example.com to example.com. In addition, a redirect from the IP address to https://example.com would be awesome.
I managed to redirect the http:// requests to https:// but unfortunately I always fail with the redirect of the www to the non-www site.
Here's my code
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com/foo2 [NC]
RewriteRule ^(.*)$ https://example.com/foo2/$1 [L,R=301]
Any ideas why it isn't working. And how do I redirect the IP address correctly?

RewriteCond %{HTTP_HOST} ^www.example.com/foo2 [NC]
RewriteRule ^(.*)$ https://example.com/foo2/$1 [L,R=301]
The HTTP_HOST is literally just the hostname, not the hostname plus the path (ie. /foo2). You mentioned in comments that the site (and .htaccess file) are in a subdirectory (/foo2), so something like the following should suffice:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://example.com/foo2/$1 [L,R=301]
The above condition states that if the HTTP_HOST starts www. then redirect.
a redirect from the IP address to https://example.com would be awesome.
As above, you just need to check the HTTP_HOST for the IP address. For example:
RewriteCond %{HTTP_HOST} =123.123.123.123
RewriteRule (.*) https://example.com/foo2/$1 [L,R=301]
Using the = operator, this checks for lexicographical equality, ie. not a regex, so no need to escape the dots.
Combined rule for all 3 instances...
However, this can all be combined into a more compact - single - ruleset. For example:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
RewriteCond %{HTTP_HOST} =123.123.123.123
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
If the request is not HTTPS, or starts with www. or is the servers' IP address then redirect...

Related

Redirect to Https exclude subdomains but regardless of WWW or not

I haven't seen this specific question on here but I'd like to know how to redirect the main site I have, regardless of whether someone includes "www" in the URL, but not redirect any subdomains. How do I do it?
This is what I have but it only works if they add the "www".
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
You may use this rule to redirect main domain with or without www:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
^(?:www\.)? matches optional www at the start.

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]

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]

Redirect from non-www to www and force SSL

I'd like to force all http traffic to https, and also always force www.
This is what I have so far:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
This seems to correctly work when the url does not contain www. So http://mydomain.com correctly redirects to https://www.mydomain.com.
However, it is not correctly redirecting to https when the www part is present. So, www.mydomain.com is not redirecting to https://www.mydomain.com
Edit
I have got this working with two rewrite blocks:
# Force ssl
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# redirect non-www to www
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
No idea if this is acceptable or not, but it works
Use your domain name and not HTTP_HOST in the rewriterule so that way it won't matter if www is there or not and you can use 1 rule. You can have two rewrite rules but just use OR. I think you were looking for is this.
# always redirect to www and/or https
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC,OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*) https://www.mydomain.com/$1 [R=301,L]

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

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]

Resources