Crawler

301 Redirect https to http or Viceversa on Apache server

Switch from http to https or viceversa using the .htaccess file.
Redirecting https to http site-wide using the .htaccess file would be done through:

 

RewriteEngine on
RewriteBase /

RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Suppose you want to 301 redirect from http to https then you’d have this:

RewriteEngine on
RewriteBase /

RewriteCond %{SERVER_PORT} !^443$ 
RewriteCond %{HTTPS}  off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
 But this doesn’t provide for preserving some urls as https and others as http.
 One way is as in this example:
RewriteEngine on

#this page has to be on https
 RewriteCond %{SERVER_PORT} !^443$
 RewriteCond %{HTTPS} !on
 RewriteCond %{REQUEST_URI} ^/secure-page1.php$ [NC]
 RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

#this page has to be on https
 RewriteCond %{SERVER_PORT} !^443$
 RewriteCond %{HTTPS} !on
 RewriteCond %{REQUEST_URI} ^/secure-page2.php$ [NC]
 RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

 

#all other pages have to be on http
 RewriteCond %{SERVER_PORT} ^443$  [OR]
 RewriteCond %{HTTPS} on
 RewriteCond %{REQUEST_URI} !^/secure-page1.php$ [NC]
 RewriteCond %{REQUEST_URI} !^/secure-page2.php$ [NC]
 RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Another way is by scripting, as described below.

So assuming you have a good reason to want some urls as https AND have them indexed, then you can add php scripting to each page and determine whether it has to be http or https.

To permit the use of php on pages suffixed as .html you need to add an Apache handler in the .htaccess file:

# parsing html as php 

AddHandler application/x-httpd-php .html 


NB: The above directive can vary greatly depending on the exact server configuration. It can be any of the below directives (or none for that matter):


Addhandler application/x+https-php5 .html

or

AddType application/x-httpd-php5 .html

or another combination like that.  It’s bewildering how many different Apache server configurations there can be and how much basic directives can differ. The trouble is if you use the wrong one, you get a 500 error. So it ends up being trial and error all the way.

Then the php script to be added at the top of every page where you need to 301 redirect from https to http, before any other line of source code would be:

<?php
 if  ( $_SERVER['HTTPS'] )
 {
 $host = $_SERVER['HTTP_HOST'];
 $request_uri = $_SERVER['REQUEST_URI'];
 $good_url = "http://" . $host . $request_uri;
 
 header( "Location: $good_url", true, 301 );
 exit;
 }
 ?>
 

Also for pages where you need to do the opposite, i.e. enforce the use of HTTPS, the php script to be added at the top of every page where you need to 301 redirect from http to https , before any other line of source code would be:

<?php
 if  ( !$_SERVER['HTTPS'] )
 {
 $host = $_SERVER['HTTP_HOST'];
 $request_uri = $_SERVER['REQUEST_URI'];
 $good_url = "https://" . $host . $request_uri;

 header( "Location: $good_url", true, 301 );
 exit;
 }
 ?>