Search code examples
apache.htaccesshttp-redirectsslhttps

301 redirect HTTPS to HTTP for a single page


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!


Solution

  • 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]