How to remove multiple redirection of homepage? - .htaccess

my website homepage redirects multiple times. I want to remove these multiple redirections.
I hit the URL in browser http://example.com
First, It redirects to http://www.example.com
Finally, Redirected to https://www.example.com
But I want to redirect from http://example.com to https://www.example.com

You can use:
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.exemple.com%{REQUEST_URI} [NE,R=301,L]
or without domain name:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Related

htaccess redirect http://www.example.com to https.example.com

I want to redirect my all URL to HTTPS.
HTTP to HTTPS redirect is ok But
http://www.example.com redirects to https://www.example.com then https://example.com. It takes 2 requests. I want to reduce the request to 1
How can I do it directly http://www.example.com to https://example.com?
I tried
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !wp-content/cache/(all|wpfc-mobile-cache)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
With your shown attempts, could you please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs. You need to push your condition to check https above redirection rule; we could handle both conditions(as per your shown urls) in single Rule here.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_URI} !wp-content/cache/(all|wpfc-mobile-cache) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Prevent redirect to www if it is approved subdomain

I am using the below the redirect from http://example.com to https://www.example.com
# redirect http://www.example.com to https://www.example.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# redirect http(s)://example.com to https://www.example.com
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
However, it also redirects subdomain.example.com to www.subdomain.example.com - how do I prevent this?
You can use this :
# redirect http://www.example.com to https://www.example.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# redirect http(s):// example.com to https://www.example.com
RewriteCond %{HTTP_HOST} !^www\. [NC]
#exclude subdomain
RewriteCond %{HTTP_HOST} !^sub\.example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect non-www and non-http to https

Yesterday I Installed SSL on the server. Since than I can't reach some pages.
www.example.com/amsterdam/shoes
example.com/amsterdam/
^ both do not redirect to https://, not even http://
www.example.com/amsterdam
^ does redirect to https://
How do I redirect all pages to HTTPS with www via .htaccess?
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R=301,NE]
This will redirect both http or non-www to https://www
Use:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
OR you can try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Redirect NON-www to www for https

I've the following redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
How can I redirect non WWW https to WWW?
The htaccess is located in the public_html folder
You can use the following :
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
This will redirect
http://example.com
https://example.com
to
https://www.example.com
I'm using this on a Joomla site and it redirects everything to https://www
I can't see this solution anywhere else so thought I'd share it
RewriteCond %{HTTP_HOST} ^domainname\.co.uk [NC]
RewriteRule ^(.*)$ https://www.domainname.co.uk/$1 [R=301,L]

htaccess rewrite http://example.com to http://www.example.com but https://www.example.com to https://example.com

Using .htaccess rewrite, I'd like to have:
http://example.com redirect to: http://www.example.com
https://www.example.com redirect to: https://example.com
Which is the best way to do this?
I've started with:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You need 2 rules:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Resources