Redirect HTTP to HTTPS Htaccess - .htaccess

I need a script to .htaccess to 301 redirect http to https, but a page needs to be accessed by HTTP. Example:
example.com/* redirects to https://example.com/*
example.com/page1 not redirect to https://example.com/page1

Just use this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
So it will just set your SSL to https for all pages, even if someone access it via http.
Use this if you want to force https for all except the directory /page1
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !page1 [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

All pages under ssl except index.html

I use .htaccess to redirect the pages to HTTP and not to HTTPS.
The code is :
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Now, I want all pages other than the index to redirect to HTTPS.
All except index.html.
How can i do this with .htaccess?
Try something like this:
# Redirect all pages, except homepage to HTTPS
RewriteCond %{HTTPS} off
RewriteRule !^(index\.html)?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
# Redirect homepage to HTTP
RewriteCond %{HTTPS} on
RewriteRule ^(index\.html)?$ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
Test with 302 (temporary) redirects to avoid caching issues.
You will need to clear your browser cache before testing.

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.

Htaccess redirect https to http for all pages except homepage, redirect https for homepage to another url

I am trying to redirect all https request to http excluding the homepage but i also want the https request for homepage to redirect to another url.
here is what i have in my .htaccess right now
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
the rule above redirects everything except homepage but i want the homepage redirected to another url e.g
https://www.example.com/
redirects to
http://www.example.com/non-secured-page
i need a full .htaccess rule that can do this.
You can use:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.+) http://www.example.com/$1 [R=301,L,NE]
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://www.example.com/non-secured-page [R=301,L]

How to redirect from http to https but no redirect if index.php file in htaccess

I am using this code in htaccess to redirect from http to https. I want no redirect if index.php file
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
How I can do that.
You can just tweak your patter in RewriteRule:
RewriteCond %{HTTPS} off
RewriteRule !^(index\.php)?$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NC]
^(index\.php)?$ will match anything but landing page and index.php.

.htaccess redirect a url to another (keep the url segments)

Need to redirect http:// www.old-url.com/any/other/url/segment
to https:/ /www.new-url.com/any/other/url/segment
Currently Im doing something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !http://www.old-url.com/1$ [NC]
RewriteRule ^(.*)$ https://www.new-url.com/$1 [L,R=301]
this results:
http:// www.old-url.com/any/other/url/segment to
https:// www.new-url.com/
Thanks
-Robbie
Use below htaccess rule to redirect http to https:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Source: htaccess redirect

Resources