https to http in .htaccess with exceptions - linux

I've done enough research to figure out that to redirect sitewide all of my https pages to their http equivalent, I need to use this code...
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
My issue is, there are a handle of pages that I still want to remain as https. For the sake of example, let's say I want page1.php, page2.php, and page3.php to REMAIN as .https, with everything else on the site redirecting. Anyone know how to do this?

Try something like this:
RewriteEngine On
# Force page1,2,3.php onto HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(page1|page2|page3)\.php https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
# Redirect other HTTPS requests to plain HTTP
RewriteCond %{HTTPS} on
RewriteCond ${REQUEST_URI} !(page1|page2|page3)\.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

Related

Force HTTP for all pages except specific URLs

I have a situation where I need to force every single page in my site to redirect to HTTP except for two specific URLs which need to force redirect to HTTPS.
The two pages that need to redirect to HTTPS pages are:
/microsoft-moc-on-demand-video-training/moc-registration-page/
/courses/register/
The code I've been using in my .htaccess file looks like this:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/courses/register/
RewriteCond %{REQUEST_URI} !^/microsoft-moc-on-demand-video-training/moc-registration-page/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(/courses/register/|/microsoft-moc-on-demand-video-training/moc-registration-page/)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Unfortunately this doesn't seem to be working. The entire site does redirect to HTTP (so part of the code works), but those two exceptions (which should redirect to HTTPS) do not do that, they stay as HTTP links.
Any idea what I'm doing wrong here?
The problem is that for RewriteRule, there is no initial / in the requested path. Therefore, you're trying to match something that isn't present.
You also had an extra / at the end of each option in the first capture group, which when combined with the final / would require a path such as /courses/register//.
The following code should suit your needs:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/courses/register/
RewriteCond %{REQUEST_URI} !^/microsoft-moc-on-demand-video-training/moc-registration-page/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(courses/register|microsoft-moc-on-demand-video-training/moc-registration-page)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.htaccess : http to https redirection does not work

I have a problem with my .htaccess and https redirection.
First of all, I have consulted these links to find a solution but none could help me.
List of links :
.htaccess redirect http to https
ModRewrite with HTTPS
https://www.ndchost.com/wiki/apache/redirect-http-to-https
htaccess redirect to https://www
https://serverfault.com/questions/116206/how-do-i-use-htaccess-to-always-redirect-from-http-to-https
When I load the page with http, the .htaccess redirect me to https but when I load the page with https, I have an infinite loop.
Here is my .htaccess code :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^(api|www|dev|stefano|sav)\.
RewriteCond %{REQUEST_URI} !^/(api|www|dev|stefano|sav)/
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*)$ %1/$1 [L]
Is anybody can help me create a redirect condition (http to https) ?
You are leaving off the Rewrite Flags You need to tell it force the redirection with R flag and optional provide status code which is recommended 301, 302 etc.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
By the way, every example you linked to shows the answer using the R flag, not sure why you didn't use exact examples :)
Solution if behind devices like load balancer.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have been fighting with this same problem for hours. It seems that I needed the solution for systems behind a load balancer. Perhaps this is because I am using Cloudflare with my site.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
After adding the above lines to my .htaccess file, I deleted the cache on Cloudflare and the site loads as it should.

Redirecting https to http expect one url with htaccess

I was trying to redirect my webpage to normal http but one specific url should be redirect to https.
I tried this:
RewriteCond %{HTTPS} off
RewriteRule ^site-name\.html$ https://domain.com/site-name.html [L,R=301]
But this way, the normal website is reachable through http and https.
Maybe you guys can help me with that.
You can have another to redirect https->http for all other pages:
RewriteCond %{HTTPS} on
RewriteRule !^site-name\.html$ http://domain.com%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^site-name\.html$ https://domain.com%{REQUEST_URI} [NE,L,R=301]

htaccess rewrite rule tweak

I have a website with HTTPS installed.
I need to ensure all pages (except the ones that are intentionally HTTPS) are forced to show on non-https.
The HTTPS is installed ONLY on domain-name.com/ssl-directory/what-ever-page-goes-here/
So, only the pages after domain-name.com/ssl-directory/ should keep the HTTPS (they do now) and all other (including domain-name.com/ssl-directory/ itself should be forced to non-https).
So far, this is what I've got but it's not working and as I'm not an expert with htaccess redirects, I don't know why.
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^ssl-directory/(.*)
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R,L]
As I said, I'm not an expert in this, but I think this should mean =>
turn RewriteEngine on
If HTTPS and
If REQUEST_URI is not a child of ssl-directory
Rewrite to same page, but with http
Obviously I'm doing something wrong, so any help would be appreciated.
Thanks
Replace your code with this:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/ssl-directory(/.*|)$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Better to use HTTP_HOST instead of SERVER_NAME and %{REQUEST_URI} has a / at the start.

Is it possible to have a certain files in a server to redirect to https and others are forced to only http no matter if you put https

Is it possible to do this on .htaccess
if(filename.php = "order.php"){
redirect to https
}else{
redirect to http
}
I need it to do something like this, user might want to put https on my index.php and its unsecure and gives an error, that why I need to force to http if its not order.php, and force https if its order.php
I have 3 similar folders like in 1 server,
I tried to use this
RewriteEngine On
RewriteRule /(order.php) https://%{SERVER_NAME}%{REQUEST_URI} [L]
but failed, I did it on PHP, but I will have to put all of it hundred files.
So Iguess .htaccess will be more time efficient,
Yes it's possible
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/order.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/order.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Resources