Question:
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:
https://mysite.com All pages on the site with https
http://mysite.com/pagina-specifica 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 meusite.com/api 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:
# BEGIN GD-SSL
<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"
</IfModule>
# END GD-SSL
# BEGIN WordPress
Answer:
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 domain.com
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.