HTTP and HTTPS redirection

4

I have a site in link and I've added an SSL certificate. I need a script via htaccess to redirect the whole site to link (301 redirect), however a single site specific page will need to be accessed via link Example:

link All site pages with https

link This page only without https

In this case, this page may even be accessed via link , but will also necessarily need to be accessed via link without redirection.

I tried this code, but it gives an error of over redirection. The intent would be to allow the mysite.com/api 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 site 301 redirect, however I am not able to include the exception so that the / api page is also accessible without 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
    
asked by anonymous 13.06.2016 / 17:01

1 answer

2

That will probably solve it. It is similar to its first version, but has been fixed != 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 were not 301, just omit the R flag:

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

See 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 parentheses have been taken from some situations, since grouping is usually done only when that piece is used in RewriteRule , with capture groups %1 , %2 etc.

    
31.07.2016 / 13:42