Remove www in htaccess - .htaccess

I have SSL secure website. I set following in .htaccess in order to always open https.
RewriteEngine On
RewriteCond %{HTTPS} OFF
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
It works fine, except the situation when somebody type www.mydomain.com, then chrome prints error with certificate.
Any suggestions how to change rewrite settings?
Thank you
Filip!

Main reasons behind SSL Certificate Error on Google Chrome are:
(1) The System Time is not the real-time.
(2) The SSL certificate has Expired.
(3) Google Chrome is not updated.
(4) The SSL certificate is not Installed properly.
(5) The SSL certificate is not issued by a Trusted Certificate
Authority (CA) or a self-signed certificate is used to secure a
website.
(6) The website is secured with an outdated 128-bit SSL.
(7) The website is secured with an outdated SHA-1 Algorithm.
https://cheapsslsecurity.com/blog/google-chrome-ssl-certificate-errors-troubleshoot-guide/
For SSL certificate without subdomain, use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

This solved my problem:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

Related

how to redirect http:// to https:// nuxtjs website hosted on heroku?

My website is build upon nuxtjs and php, We added ssl certificate and domain became https, but when user login to http:// website goddady shows them that this domain is parked free, courtesy of GoDaddy.com.
I wrote that I should add htaccess file and here is what I did
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#Heroku way
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And I put this file to main directory of the source code,
but even though it doesn't work, Am I doing something wrong here?

Redirect HTTPS to HTTP on localhost

I have an SSL certificate on the production server of my website, but when I run the website site on the local server I see a security message because there is no certificate locally.
What can I add to the htaccess file to prevent HTTPS from being run on the Localhost?
My current htaccess code is below:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
It's unclear what you're asking.
If you're asking how to prevent http://localhost/some_path from being rewritten by Apache mod_rewrite to https://www.localhost/some_path, then
try adding this just before RewriteRule:
RewriteCond %{HTTP_HOST} !localhost

.htaccess How to Exclude One Site From SSL?

I'm trying to exclude one site from using SSL, but I can't seem to get the directive right. I want all my AddOn domains to use SSL except example.com. (because StartSSL is difficult to get along with)
# Enable https
RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}:443%{REQUEST_URI}
RewriteRule !^example.com(/|$) https://%{HTTP_HOST}:443%{REQUEST_URI}
What you're trying to do will not work. The SSL handshake is initialized before any request data is sent to the server, therefore, you're going to get a certificate exception if someone goes to https://example.com no matter what you do.
If the certificate exception isn't your concern, then you need to match against the %{HTTP_HOST} variable:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
then if you need:
RewriteCond %{HTTPS} on
RewriteCond ^{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force all requests to use https:// and prepend www

I have spent countless hours trying to solve this problem and it's doing my head in. I'm sure that it is very simple but none of the answers I have found either on stackoverflow.com or other sites work for me.
I am using an SSL certificate that is only valid for www.example.com and for a myriad of silly corporate reasons I am stuck with only this certificate.
I would like to force all requests to use SSL and prepend www. to the domain. All of the following domains rewrite correctly:
example.com/
http://example.com/
http://www.example.com/
https://www.example.com/
The domain that is not rewritten and is the one problem I cannot find the answer to anywhere is
https://example.com
It simply spits out the usual "Certificate Is Not Valid" warning.
I have the following code in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
I really hope that somebody has a solution for me because I cannot find the answer anywhere.
This should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=302]
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=302,L]
It will check to see if it has www in the url, if it doesn't then it will add it. Then it will check and see if it is https and if it isn't then it will add it.

Can't get my htaccess to work on a 301 redirect

I’m trying to redirect an https://www.domain.ext to a plain https://domain.ext, but just can’t get it to work; this is where I’m right now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^https\:\/\/www\.cadenanoticias\.mx$
RewriteRule ^/?$ "https\:\/\/cadenanoticias\.mx" [R=301,L]
Allso tried
RewriteEngine on
RewriteCond %{HTTP_HOST} *!^https\://www*.cadenanoticias\.mx [NC]
RewriteRule (.*) https://cadenanoticias.mx/$1 [L,R=301]
And not working see: https:www.cadenanoticias.mx
Any idea on why it’s not working?
UPDATE FIX
Got it to work as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.cadenanoticias\.mx$ [NC]
RewriteRule ^(.*)$ https://cadenanoticias.mx/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^www\.cadenanoticias\.mx$ [NC]
RewriteRule .? https://%1%{REQUEST_URI} [R=301,L]
Hope it helps someone else.
%{HTTP_HOST} does not match against protocol. Use
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.cadenanoticias\.mx$ [NC]
RewriteRule ^(.*)$ https://cadenanoticias.mx/$1 [R=301,L]
EDIT :
No, bypassing the SSL certificate validation is not possible. The SSL handshake precedes htaccess rules for security reasons. If this was possible, a hacker could hijack a SSL connection to an insecure one without really needing a valid certificate.
There's no solution for this other than buying a cheaper certificate just to do handshake and redirect.

Resources