htaccess – HTTP and HTTPS redirection with exception


I have a website on http and added an SSL certificate. I need a script via htaccess that redirects the entire site to https (301 redirect), but a single site-specific page will need to be accessed via http: Example: All pages on the site with https Just this page without the https

In this case, this page can even be accessed via https , but it will also necessarily need to be accessed via http without redirection.

I tried this code, but it gives excessive redirection error. The intention would be to allow the page to be accessible both via http and via https without redirection.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_URI} !^/(api) [NC] #permite tanto acesso via https quanto http
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This other code below worked perfectly for the site's 301 redirect, however I'm not able to include the exception so that the /api page is also accessible without the https:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^meusite\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
# BEGIN WordPress


This will probably do the trick. It looks like its first version, but fixed a != and https in RewriteRule

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/api.* [NC] #essa linha é a exceção
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If the redirect was not 301, it would be enough to omit from the R flag:

RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

See just an example if the exception was a specific domain, for example:

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC] #exceção pra
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Note that the parentheses were removed from some situations, as usually only groupings are made when that piece will be used in the RewriteRule , with capture groups %1 , %2 etc.

Scroll to Top