Problems with HTTPS and WWW Redirection

2

I'm having trouble with one thing, I have several conditions to answer in my .htaccess but I can never answer them at once, just one or the other.

I have a project in Laravel in the path /public_html/portal/ to display need to redirect the user to the /public_html/portal/public folder until then, okay, I have a .htaccess that does this. It is the same as below:

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^(|chat|infinite|cliente|cadastro)\.dominio\.com$ [NC]
RewriteRule ^(.*)$ portal/public/$1 [L]

The second line is for direct access to their domains, because I work with wildcards in my project.

The problem now

Based on this rewrite of line 3 how can I accomplish these two things:

  • How can I force HTTPS.
  • Redirect all accesses with https://www.dominio.com to https://dominio.com
  • asked by anonymous 03.05.2017 / 13:37

    2 answers

    2

    As I replied in link , just change the last two lines:

    # Redireciona para o HTTPS independente do domínio
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]
    
    # Remove www. no prefixo do domínio
    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,QSA,L]
    

    Notes:

    • The %1 takes the value of (.*) within ^www\.(.*)
    • R=301 makes the permanent redirect
    • The QSA is for the querystring (although it may not be necessary)
    • The L is so that both RewriteRules do not run at the same time
    03.05.2017 / 16:42
    1

    Buddy, I would not use htaccess to redirect to HTTPS no. I find it easier for you to do this through the application itself, through a Middleware (if you are using Laravel 5).

    The code comes down to this:

      if (! Request::secure()) {
             return Redirect::secure(Request::path());
      }
    
        
    03.05.2017 / 16:33