http to https causing redirect loop - apache

I wish to forward all visitors to my site to use HTTPS instead of HTTP. Whatever they request, I want to server over HTTPS.
I've tried adding this to my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
But it just causes a redirect loop. I'm guessing RewriteCond isn't functioning as expected?

This should work :
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,NC,R,L]

Related

Using APACHE REWRITE To Force One Specific Page To Be HTTP (.htaccess)

I have tried using some rewrite to change my /welcome.php page to http. it is located in the root directory, but what I have done has seemingly forced all pages on my site to http instead of just the one. Here is what I have tried:
RewriteEngine On
#Force remove WWW
RewriteCond %{HTTP_HOST} ^www.w5unt.ga
RewriteRule (.*) http://w5unt.ga/$1 [R=301,L]
#Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As you can see I am also force removing WWW from all urls at the same time I am trying to force my one page (welcome.php) to be http. I believe the error is in this bit of code
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
However I am not sure how to syntactically correct my issue, any advice?
Try with below I've edited the part of your rule and made a check to exclude welcome.php.
RewriteEngine On
#Force remove WWW
RewriteCond %{HTTP_HOST} ^www.w5unt.ga
RewriteCond %{REQUEST_URI} !^/welcome.php
RewriteRule (.*) https://w5unt.ga/$1 [R=301,L]

HTTPS redirect issue

I need to force all request to HTTPS rather than HTTP.
For that I have added the following code in to my .htaccess as can be seen below:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This URL is forced to HTTPS: http://localhost/test
but this URL is not forced to HTTPS: http://localhost/test/public/assets/css/custom.css
This is straight from the Apache documentation:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
It doesn't look much different than yours but perhaps the minor changes will make a difference with your specific issue.

Redirection from https to http not working

I've tried with multiple conditions to redirect all my https pages to http as my SSL certificate has expired. As it is taking time to reflect the changes, I want to redirect all those requests made by https to be called over http.
Tried with these rules:
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} ^on$
RewriteRule ^(.*)$ http://my_site.com/$1 [NC,L,R]
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Nothing seems to be working. Is there a better way to handle this?
It's solved by placing the rules at the end of the file instead of placing them at the starting of the file

Redirect to HTTPS and rewrite to local app in .htaccess

I have a node.js app on a VPS. The only way my VPS provider allow to use node.js is creating an .htaccess file with these rules:
RewriteEngine on
RewriteRule ^(.*)$ http://localhost:8080/$1 [P]
I don't access site from localhost, but from public accessible domain - like http://example.com.
If I use rules above It's works as expected. But I also need to redirect all trafic to HTTPS. So I made made these changes:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://localhost:8080/$1 [P]
But what I got is the exact behaviour as the previous rule. It's redirecting trafic to localhost, but it doesn't force the browser to use HTTPS.
Please do anyone knows where's the problem? Thank for your suggestions.
Try this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://localhost:8080/$1 [P]

Apache redirect .net, .org, .co to .com using Rewrite or serveralias

I have a website. I bought all (mydomain.com|net|org|co) domains for my website. Now, I want to redirect all my requests to mydomain.com.Presently, in my httpd.conf, I have ReWrite rules for forwarding http requests to https.
For forwarding all http requests to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I combined my existing settings with this answer and I tried following code:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} !^example.org$
RewriteRule ^ http://example.org%{REQUEST_URI} [L,R=301]
It is working fine for http requests but not for https(https://mydomain.net/info). I got SSL certificate for mydomain.com. It is displaying me This Connection is Untrusted page.
What would be best way to handle this scenario. Can anyone guide me on this
Thanks in advance,
deadMan.
Try this:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com%{REQUEST_URI}
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301]