htaccess https www (only) to https non-www (only) - .htaccess

How can I rewrite all traffic using "https" and "www" to "https" without the "www". I do not want any rewrites done on non-ssl traffic (besides the existing code below). I also already have a rewrite for clean urls going on at the same time. The SSL cert is purchased on the non-www URL which is why I am asking how to do this.
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks for your time.
[EDIT]
Ended up getting this to work:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule ^(.*)$ https://mysite.com/$1 [L,R=301]

Related

Domain too many redirection

Hi after 1 week of research i'm still stuck with my domain redirections
I want my http://domain.fr pointing to https://www.domain.fr
I'm trying with htaccess file with no success...
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.*
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
When i'm testing my redirections it seems https://www.domain.fr is redirecting itself without understading why...
have you guys any idea ?
The problem is in your 2nd RewriteRule
RewriteCond %{HTTP_HOST} ^www\.*
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
You rule does a permanent redirection of www domain to the same location ie : ( www.e ample.com => www.example.com ) that is why you got the redirect loop error.
To redirect http urls to https ,first of all you need to check if the request scheme is http. You can use RewriteCond %{HTTPS} off to check the non - secure http connection and then use RewriteRule to redirect the request to https .
Use the following simple Rule to redirect http to https :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Make sure to clear your browser cache before testing this.

Redirect to non www is not working for pages in https

I am trying to add redirect rules using .htaccess for such goals:
Redirect all http pages to https.
Redirect all www http and https pages to non www https.
My .htaccess code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Everything is working good except one:
http redirects to https (all pages)
https www redirects to https non www (main page and subfolders)
But https://www.example.com/1/page.html does not redirect to https://example.com/1/page.html (both pages open)
What is the problem? How to write an .htaccess rule to redirect all the pages to https non www?
You can use this to remove www and force https:
RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your cache before testing this. Combing the two rules will help to speed up your website too, instead of having to run two seperate rules.

https redirection not working for home directory

I'm currently trying to force https for may domain name. Its working for mydomain/wildcard but not for the home url that is mydomain/
I'm using this line for redirection in my .htaccess file:
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You're almost there, use this in your .htaccess:
RewriteEngine On
#Force HTTPS on everything
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
That will force HTTPs on everything

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]

Remove SSL integration from a specific folder using htaccess

I have an integrated SSL for my entire site and have placed htaccess code to redirect to https when anyone visits my domain URL. But I want to keep one folder out of this redirection to https. Please help me with this... Below is the htaccess code placed in my root to redirect all requests to https counterpart
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Thanks
Just add a condition to exclude the folder:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And if you wanted to redirect SSL requests to non-SSL for /folder1, then:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/folder1
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Resources