Redirect to NON Secure site utilizing PHP or .htaccess

I am using the following code to try and direct my site to NON SSL but I get a redirect loop. I have tried this with .htaccess and PHP.

PHP Version

if($_SERVER["HTTPS"] == "on") {
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);

.htaccess Version

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

How can I do a simple redirect. “If the request is secure, send to non secure”?


It’s not {SERVER_NAME} but {HTTP_HOST} that you should be using. Then, in the .htaccess versino, you don’t need RewriteBase / nor the = (that’s the default condition) in the RewriteCond. As a tip, too, ^.*$ says to match all while all you really need is match anything (optional), i.e., .? (you don’t need the start or end anchors because you’re not capturing anything).

Note: The PHP version will only redirect its script while .htaccess will redirect everything (as specified).