Forcing http using .htaccess - php

This is the script I have right now, how do I have my script force all traffic to http, currently it is doing the exact opposite, it is forcing all traffic to https.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I've also tried this and it didn't work
RewriteEngine On
RewriteCond %{HTTP} !=on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I got this error:
Too many redirects occurred trying to open www.blankpage.com .

You want to check that HTTPS is on:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And if it is on (%{HTTPS} on), redirect to http://. There is no mod_rewrite variable called %{HTTP}, only %{HTTPS} which can be "on" or "off".
The reason why you were getting the too many redirects error is because:
RewriteCond %{HTTP} !=on
is always true no matter if the request is http or https, since the variable doesn't exist, it will never be equal to "on". Therefore, even if the request is http, you keep getting redirected to the same URL (http).

In the processwire htaccess look for these lines
# -----------------------------------------------------------------------------------------------------------------------------------------------
If you only want to allow HTTPS, uncomment the RewriteCond and RewriteRule lines below.
# -----------------------------------------------------------------------------------------------------------------------------------------------
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
# -----------------------------------------------------------------------------------------------------------------------------------------------
If you only want to allow HTTP use the code below
# -----------------------------------------------------------------------------------------------------------------------------------------------
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

Related

http request should be redirect to https

I have Installed SSL on my server.
So request are https by default.
But when I enter Url http://example.com/abc/a it redirects to
https://example.com it should be go to (https://example.com/abc/a).
And when I tried with exact secure Url https://example.com/abc/a it
redirects to same.
I want to solve my first case.
You need to add following code at top of your .htaccess file, if you are using Magento
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
You have not shown what you have in your .htaccess file but I use the following to redirect all standard http traffic to https.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

How to redirect site to https and http using htaccess for specific file and folder

I have a wordpress site that I wanna change https to http for only '/wp-admin' and 'wp-login.php'. I wanna use .htaccess to achieve it, but I m not good at regular expressions. Below is my current htaccess codes.
# Force HTTPS for /my
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} !^[A-Z]+\s/wp-admin [NC]
RewriteRule !^wp-admin https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Force HTTP for anything which isn't /my
RewriteCond %{HTTPS} =on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/wp-admin [NC]
RewriteRule ^(wp-admin) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
Currently it works for wp-admin/, but not for /wp-login.php. How to modify this?
Replace your Rules with this :
RewriteEngine on
#http to https except "/wp-admit" and "/wp-login.php"
RewriteCond %{HTTPS} !on
RewriteRule ^((?!wp-admin.*|wp-login\.php).*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
#Redirect "/wp-admin" and "/wp-login.php" to http if accessed using https scheme
RewriteCond %{HTTPS} on
RewriteRule ^(wp-admin.*|wp-login\.php)$ http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
Clear your browser cache before testing these rules.

Force SSL HTTPS for subdomain with .htaccess

I have this code for forcing SSL on a website generally:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Which I got from here
I would like it to only apply to a specific sub-domain.
For example, if you were to visit http://sub.domain.org it should redirect to https://sub.domain.org.
I have a hunch that I need to add another RewriteCond which basically says is the domain that is being hit is http://sub.domain.org, but I am at a loss on how to write this code.
I've been looking at other stackoverflow questions such as:
Force HTTPS and WWW for domain and only HTTPS for subdomains HTACESS
Force SSL/https using .htaccess and mod_rewrite
and the Apache docs: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
And I've had these attempts:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(reports\.)?uksacb\.org$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
Sort of reversed the answer in this question/answer:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} =reports.uksacb.org
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
However I'm still failing to make sense of how to do it correctly.
If anyone could explain to me what I'm doing wrong, what I need to do to get it right and how does one go about debugging their rewrite conditions and rules to make sure it works properly I'd be very grateful. Sometimes I think my browser is caching the rules I've written and my different attempts are not taking any affect!
My whole .htaccess looks like this:
Options -MultiViews
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
To avoid using the domain or subdomain names I used to use the following code:
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I prefer without WWW.
%{HTTPS} Set to on if the request is served over SSL.
%{HTTP_HOST} Everything between the protocol (http:// or https://) and the request (/example-file.txt).
%{REQUEST_URI} Everything after the server address — for example, a request to http://example.com/my/example-file.txt sets this value as my/example-file.txt.
%{HTTP:X-Forwarded-Proto} The protocol used when the URL was requested — set to http or https.
I hope it can be useful for anybody.
For debugging I use a website I found https://htaccess.madewithlove.be/
Using the rules you provided
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I got an error at
RewriteCond %{HTTPS} !=on
This might work for you
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
Found the above example at https://www.sslshopper.com/apache-redirect-http-to-https.html

multiple htaccess redirects with http to https but except page

i use mod_rewrite to allow "friendly URLS" to replace search parameters, which works fine.
However i need to now redirect all traffic to https:// but allow only some pages to remain on http://
(i need to do this so my referrals are collected by the sites i send traffic to)
Over the years i have added to the .htaccess file, most works OK but i dont fully understand it, so it may be getting messy too :-(
i have the below (i have cut out anything i dont think is relevant)
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([A-Za-z0-9'/-]+)-([0-9]+)$ profile.php?id=$2 [NC,L,QSA]
# NOTE: this allows for profile.php?id=123 to be replaced with /profile/name-123
# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
The above works OK, but redirect ALL pages to https
I have tried adding /changing the https redirect part to the below to allow urlout.php not to be redirected, but this just loops and i get a browser warning that the page has too many redirects :-(
# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteCond ${REQUEST_URI} !^/urlout\.php
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have tried a few other things too, but they either dont redirect and everything goes to https, or i get a loop error or internal 500 error...
Basically i want all pages to go to https, but not urlout.php
Any advice on any of the code above would me much appreciated!
Answering my own questions:
i managed to do this, was a silly mistake of me not adding [NC] after the first condition...
However, for others, the below code will redirect ALL to https:// from http:// with the exception of 1 page (urlout.php in my case)
# redirects any http:// traffic to https:// with exception of urlout.php
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} !^/urlout\.php [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I needed to do this, so that sites i send traffic to can see my domain as the "HTTP referrer".
I however found a simpler way to pass the referrer from https to http (although browser specific!) of adding to the HTML head:
<meta name="referrer" content="origin">
My ref for this was (among others): moz.com's Meta Referrer Tag Blog Post
If you want to redirect some spacific pages to https, forexample, To redirect :
http://www.example.com/page.html
to
https://www.example.com/page.html
And
http://www.example.com/page2.html
to
- https://www.example.com/page2.html
You may try the following :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]
If you want to redirect the whole site from http to https, you can use the following :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]
RewriteCond %{HTTPS} off is important to avoid redirect loop as it skips the rule for https requests.

Htaccess redirection to https

I am using the following httaccess content, to force rewrite the url's to use https instead of http
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L]
But it's always getting,
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address
in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept
cookies.
I've used many other solutions but always getting this...
Try the code below for .htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
#uncomment the below two line if you need to append www
# RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Categories