What I try is to redirect (if you type it directly into the addressbar) to https://example.com
thus
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
example.com -> https://example.com
www.example.com -> https://example.com
I tried already (and other lots of other ways):
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
So
http://www.example.com
https://www.example.com
www.example.com
will all redirect to https://example.com
BUT example.com will not automatic redirect to https://example.com
How can i acomplish that?
You can use an OR condition to match against both requests www or http and redirect it to https://non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I think #starkeen s' way will work... but not when you're using Cloudflare!! The solution for people who using Cloudflare:
Force HTTPS By default CloudFlare HTTPS doesn't force all the traffic
to HTTPS. To force HTTPS do the following:
Go to your websites Click on the gear icon and select "Page rules"
Enter your website in the URL pattern input Toggle the "Always use
HTTPS" switch Click on "Add rule" at the bottom By doing this all the
traffic of your website will use HTTPS. When someone access your
website using HTTP, CloudFlare will redirect those request to HTTPS.
It worked for me.
Related
I want these redirects
http://example.com => https://www.example.com
http://www.example.com => https://www.example.com
https://example.com => https://www.example.com
https://example.com/asa.php => https://www.example.com/asa.php
http://subdomain.example.com => https://subdomain.example.com
https://www.subdomain.example.com => https://subdomain.example.com
What I have tried is
#redirect http or https to https//www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But these rules handle all domains of the site, not just the main site.
The idea is to make all the incoming http to https.
And all main site non-www to www.
And for subdomain from www to non-www if any.
All in all, these are three separate requests.
change http to https, this should already work as is
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
redirect main site example.com to www.example.com. This can be done by being more specific
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L]
redirect non main sites from www.subdomain.example.com to subdomain.example.com. To strip the leading www, you can capture the part after www., and use the captured subdomain in the rewrite rule
RewriteCond %{HTTP_HOST} ^www\.(.+\.example\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
Finally, put 1. at the end to avoid two redirects for e.g. http://example.com or http://www.subdomain.example.com
Unrelated, but never test with R=301!
I have a SSL certificate for non www URL: https://example.com
I know there are many similar question, but none of them seems to solve my problem. Here is what I am trying to do:
Problem:
1) http://www.example.com -> https://example.com **www to non www DONE**
2) http://example.com -> https://example.com **http to https DONE**
So far, my .htaccess file looks like so:
# www to non www
# http to https
# SSL off
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^http\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# https://www to https://
Does anyone know the solution? With the current solution, I only get the following error message from the browser:
Corrupted Content Error
The site at http://example.com/ has experienced a network protocol
violation that cannot be repaired.
The page you are trying to view cannot be shown because an error in
the data transmission was detected.
Please contact the website owners to inform them of this problem.
Here are the solutions that I already tried:
.htaccess redirect www to non-www with SSL/HTTPS
htaccess force https and redirect www to non-www, but no other subdomains
MANY MORE
Change your rules to this:
<IfModule mod_rewrite.c>
RewriteEngine on
# https://www and http://www to https://non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]
# http://non-www to https://non-www
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com/$1 [R=301,L]
</IfModule>
I'm trying to set up my .htaccess file to redirect all traffic to https://www.example.com using the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
But it doesn't work correctly in all instances.
It correctly redirects the following:
http://www.example.com
http://www.example.com.au
http://www.example.net
http://example.com
http://example.com.au
http://example.net
https://www.example.net
It does not redirect the following:
https://www.example.com.au
https://example.com
https://example.com.au
https://example.net
Any help would be greatly appreciated. Thanks.
To redirect to https://www you can use the following
RewriteEngine On
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
This will redirect all http or https urls to https://www.example.com .
Make sure to clear your browser cache before testing this 301 Redirect.
I have spent few hours to find a answers for a simple question, but I didn't. With htaccess on apache server, I want:
1-All traffic to be HTTPS
http://example.com >> https://example.com
(edit) AND ALSO
2-All traffic to a subdomain to be redirected to a specific port
subdom.example.com >> subdom.example.com:1234
I have created this piece of crap, but doesn't work in all the cases
RewriteEngine On
RewriteCond %{SERVER_NAME} ^subdom.example.com$
RewriteCond %{SERVER_PORT} !^1234$
RewriteRule ^(.*)$ https://%{SERVER_NAME}:1234%{REQUEST_URI} [L,R=301]
# If not https, then force it
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Finally, nice to have would be to always redirect example.com to www.example.com
Thank you all for your support
I would like to make it so that all requests to my website are redirected to HTTPS with no www. Right now I apparently have both a www and non www version of my site which is supposed to be bad for seo (duplicate content). I also just added an SSL certificate and need to redirect all traffic to the https version of my site. Below is an example of what I am looking to accomplish:
example.com -> https://example.com
www.example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
How would I accomplish this within my htaccess file?
try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
These rules need to be at the top of the htaccess file in your document root
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]