301 redirect HTTPS to HTTP for a single page - apache

I currently have a SSL certificate applied to my site and ALL URLs redirect to https correctly. I need one of the URLS to be HTTP. I have the following code in my .htaccess that redirects all pages to HTTPS.
I would like the following URL below to be HTTP and NOT HTTPS.
http://www.example.com/blog_rss.php
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Thanks in advance for your assistance!

You can replace your current code by this one in your htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/blog_rss\.php$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteRule ^(blog_rss\.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]
EDIT: looks like %{HTTPS} is not recognized on some servers, which is causing an infinite loop.
Try with %{SERVER_PORT} (if default http port is still 80 and ssl port is 443)
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/blog_rss\.php$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(blog_rss\.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]
You could also try with your initial syntax
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} !^/blog_rss\.php$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteRule ^(blog_rss\.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]

Related

Am I missing something for .htaccess redirect to https?

I'm trying to get all traffic to direct from http to https:
www.example.com -> https://www.example.com
example.com -> https://example.com
The server is an AWS EC2 instance running LAMP on Ubuntu 16.04 (setup tutorial used).
The SSL certificates were generated by Let's Encrypt for both example.com and www.example.com (tutorial used).
I've gone through a dozen SO answers and other tutorials, and none of the .htaccess example code provided work for me. Here is a list of a few I have tried, without any success (I've tried uploading the file through SFTP and SSH editing through nano):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
,
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
,
RewriteEngine On
# https/http www -> https non-www
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# http non-www -> https non-www
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
,
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
,
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
,
RewriteEngine On
RewriteBase /
https://www.primesoft.in
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.%{SERVER_NAME}/$1 [R=301,L]
Any help would be deeply appreciated.
After more research, it turns out Apache by default has .htaccess turned off. This answered solved it:
.htaccess not being loaded in Ubuntu 14.04 (Apache 2.4.7)

How to prevent subdomains from redirecting to https - htaccess?

I am trying to work out how to redirect all http instances to https:// though excluding the subdomain.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example.co [NC]
RewriteRule (.*) https://example.co/$1 [L,R=301,QSA]
For example, domain.example.co should be left as it is and not redirected to https://.
example.co or example.co/sub/sub should be redirected to https://
I have tried changing the RewriteRule:
RewriteRule ^$ https://example.co/$1 [R,L]
This leaves subdomains as they are but has no effect on subdirectories under example.co - i.e. example.co/sub/sub.
How could I redirect from http to https but exclude all subdomains?
Note
I also have a rewrite rule which points subdomains to their directories without changing the URL:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
sub.example.co will displays example.co/sub but the URL will not change
You can try this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.+\.)?domain\.com$
RewriteRule ^(.*)$ https://%1domain.com/$1 [R,L]
Or This :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R,L]
Note: Replace sub with subdomain and domain with domain name
Enjoy it ;-)
Try below I am assuming you are not using www appended to host.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
ProxyPassReverse / http://example.co/

Rewrite HTTPS domain to another HTTPS domain - htaccess

Currently I have
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^domain2\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
Which redirects www and non-www domain.com to https://domain.com
And redirects www and non-www domain2.com to https://domain.com
domain2 being a parked domain.
But you can still access https://domain2.com and https://www.domain2.com
I want these redirected to https://domain.com
Please advise, thanks
You can combine some of these rules and modify redirect rule for domain2 -> domain to always redirect to https://domain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain2\.com$ [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,NE,L]
It should be as simple as this:
RewriteCond %{REQUEST_SCHEME} !https [NC,OR]
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,NE,L]
Still, keep in mind the last comment from #anubhava about configuring VirtualHost and SSL for domain2.com

Redirect all website link www to non www and http to https using .htaccess

When I go to https://www.example.com/webinars.php.
I want to be redirected to https://example.com/webinars.php
But Its not working.
My current .htaccess file is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
Is this what you want?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Redirect https to non http and Http to Https in directory with htaccess

I want to make my homepage to redirect to non https if it https
if https://example.com will redirect to http://example.com
and i want to make directory with ssl (just directory using SSL)
if http://example.com/directory wil redirect to https://example.com/directory
I'm use this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
but it just show loop
RewriteEngine On
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} !^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} ^off$
RewriteCond %{REQUEST_URI} ^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]