Htaccess Redirect / and index.php to HTTPS - .htaccess

I am trying to redirect:
http://website.com & http://website.com/index.php to https://website.com
All the solutions I found will redirect http://website.com/test.php (that has login) to a 401 page. I just want / and /index.php to redirect to HTTPS
The following code does not work for my needs:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
and
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

You may use this rule as your topmost rule:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^(?:index\.php)?$ https://%{HTTP_HOST} [L,NE,R=301]
Make sure this is top rule and you test it from a new browser.

Related

RewriteRule for subdomain to subfolder .htaccess

I'm trying to accomplish the following:
non-http force to https - works
www force to non-www - works
website loaded from subfolder (/web) - works
test.example.com load different subfolder (/test) - does not work
4, Does not work, the condition is met to go to /web. Can't understand how to change this into /test
the .htaccess code I use:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ /test/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteRule ^(.*)$ /example/web/$1 [L,NC,QSA]
With your shown samples please try following htaccess rules file here. Please make sure to clear your browser cache before testing your URLs. In case you have further more rules(apart from shown ones) then make sure these Rules are before those rules.
RewriteEngine On
##Apply https to uris here..
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
##Apply non-www to uris here..
RewriteCond %{HTTP_HOST} ^(?:www\.)(example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ example/web/$1 [L,NC,QSA]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^test\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ www.example.com/test/$1 [L,NC,QSA]

Redirect Any Domain from non-www to www and to HTTPS without double redirect

I have the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
to redirect from non-www to www
On this question, I found the following code, to make such redirection, and also http to https redirect, without casuing a double redirection:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
But I don't want the example.com part, I need a flexible code.
So I tried this code:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,NE,L]
something in syntax is bad, and it doens't work as expected.
You can use:
RewriteEngine on
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]
Just use this in your .htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
Using %{HTTP_HOST} will grab the host instead of implicitly calling example.com. Make sure you clear your cache before testing this.
EDIT BASED ON COMMENTS
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%1.%2/$1 [R=301,NE,L]

htaccess redirect http and www to https non-www with one rewrite rule not working so

I had the following .htaccess code working with 2 RedirectRules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_METHOD} !=POST
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
But I want to make an optimized solution.
I came up with this based on my previous htaccess, but this is not working, and I would like to know why?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
Another variation (preferred), not working:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
Browser gives "Corrupted Content Error" message.
You may be able to use this combined rule:
## remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure to use a new browser for testing the change.

Redirecting www to non-www, http to https and domain.com to domain.com/index.php

Am trying to use .htaccess to redirect:
1) all www to non-www e.g. www.example.com/page.php to example.com/page.php
2) all http to https e.g. http://example.com/page.php to https://example.com/page.php
3) the domain (and subfolders) to their index.php page e.g. https://example.com to https://example.com/index.php and https://example.com/en-gb/ to https://example.com/en-gb/index.php
I can get 1) and 2) working correctly with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
But I am struggling with including 3) as well. I have tried this but no luck:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
Update - tried this but no luck
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
Redirect / https://example.com/index.php
Second update - tried this but same as before I get index.php added recursively to the URL:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://example.com/index.php$1
Clear your browser cache and put the following code at your main directory .htaccess file , no need to go every folder and put code there :
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://yoursite.com/$1 [L,R=302]
# the two lines above will catch both http://www & http:// and force https://
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*) https://yoursite.com/$1 [L,R=302]
# the three lines above will catch only https://www and redirect it to none wwww
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^ /index.php [R=302,L,NE]
# the two lines above will force index.php to domain when request to index.php comes without it
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/$ /$1/index.php [R=302,L,NE]
# the two lines above will force index.php to any directory when request to index.php comes without it
Test it , if it's oK , change every 302 to 301 to get permanent redirection
Well - thanks for your help everyone :)
I eventually got there. In case this helps anyone else with the same problem... this went in the root directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^$ https://example.com/index.php
And this in each folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{REQUESTFILENAME} !-f
RewriteRule ^$ https://example.com/en-gb/index.php

htaccess which allows both http and https - change to allow/redirect to https only

I have inherited the following htaccess, and am having issues getting it to always redirect to https.
Currently it allows for both http://www and https://www, but I'm wanting it to always redirect to https://www (subdomains should always redirect to https://subdomain.example.com)
All my attempts so far have caused 500 errors, so I'm obviously missing something.
AddDefaultCharset UTF-8
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} !sub/
RewriteRule (.*) /sub/$1 [L]
You can tweak your rules as:
AddDefaultCharset UTF-8
Options +FollowSymLinks
RewriteEngine On
# handle sub-domains
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# handle main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub/ [NC]
RewriteRule (.*) sub/$1 [L]

Resources