Redirect www to non www with SSL - .htaccess

I have seen many articles on rewrites, but I have a problem.
example.com => https:// example.com (OK)
www.example.com => https:// example.com (OK)
https:// example.com => https:// example.com (OK)
https:// www.example.com => https:// example.com (FAIL)
He tried several forum solutions, but I have the same problem. Now my htaccess is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
</IfModule>

You can use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
</IfModule>
In your code, the second test is not performed with http, and %1 is not correct.

Related

Htaccess redirect from www to non-www, HTTP to HTTPS, and index.php to root

I'm trying to redirect url variations so that something like this:
http://www.example.com/index.php
redirects simply to: https://example.com
(ie http to https; www to non-www; and index.php to root of the domain)
# Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example/$1 [L,R=301]
# direct /index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://example.com/ [R=301,L]
</IfModule>
Question:
Is there a more elegant solution that combines the two?
The code above for redirecting from www to non-www and from HTTP to HTTPS is correct and will always deliver the client to the correct address through one redirect. However, redirecting /index.php to / after that can result in a needless additional redirect.
ie. if history will go like this:
> http://www.example.com/index.php [301]
> https://example.com/index.php [301]
> https://example.com/ [200]
You can fix this by simply reversing the order just like this:
<IfModule mod_rewrite.c>
# direct /index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ https://example/%1 [R=301,L]
# Canonical HTTPS/non-WWW
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example/$1 [L,R=301]
</IfModule>

How to forcefully redirect http and www to https

I am currently using the folowing htaccess to redirect forcefully to https. But this is not redirected to https when the URL is http://www.myexample.com.
How can I change to :
http://www.myexample.com -> https://www.myexample.com
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^myexample\.com [NC]
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.myexample.com/$1 [R=301,L]
</IfModule>

Minimize redirects for www and HTTPS

I want to redirect every request do https://www.example.com/request
It's already working, but I want to minimize the redirects. Currently I got 2 redirects: http://example.com -> https://example.com -> https://www.example.com
Every other case is working with 0 or 1 redirect.
My .htaccess contains the following (generated by "Easy HTTPS (SSL) Redirection" WordPress Plugin):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Got it for my own
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^example\.com.* [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com.*
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]

htaccess file not redirecting a certain url

I added an SSL to my site and have the following htaccess file in place to redirect:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
</IfModule>
This covers almost everything, so http://www.example.com, http://example.com, and example.com all forward to https://example.com. However, there is one scenario where it does not. If I enter https://www.example.com, it does not redirect to https://example.com and I get a security certificate error. Looking for a solution. Thank you in advance.
cdr6545
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
# To redirect from www to non www (Rewrite www.example.com → example.com)
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http%{ENV:protossl}://%1/$1 [R=301,NE,L]
# Redirect HTTP to HTTPS automatically (only if not in localhost)
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Redirect all http and https://www to https://example.com via htaccess

I have the following htaccess code:
<IfModule mod_rewrite.c>
# enable Rewrite engine
RewriteEngine on
# redirect http to https
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
I am already did the redirect from http:// to https://, but I would like to redirect also (for seo reasons), all URLs from https://www to https
but I have no idea how this can be done!
You can use:
RewriteEngine on
# http(s) www -> https
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# http -> https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Resources