Problems with HTTPS and WWW Redirection

Asked

Viewed 397 times

2

I’m struggling with something, I have various conditions to meet in mine. htaccess but I can never answer them at once, just once or twice.

I have a project in Laravel on the way /public_html/portal/ to display it I need to redirect the user to the folder /public_html/portal/public So far so good, I have a . htaccess that does it. It follows the same below:

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

The second line serves to directly access the respective domains, because I work with wildcards in my project.

The problem now

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

  1. How can I force HTTPS.
  2. Redirect all access with https://www.dominio.com for https://dominio.com

2 answers

2

How did I respond in /a/184459/3635, 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 (.*) inside ^www\.(.*)
  • The R=301 makes the redirect permanent
  • The QSA is for querystring (although it may not be necessary)
  • The L is so that both Rewriterules do not perform at the same time

1

Buddy, I wouldn’t wear it htaccess to redirect to HTTPS no. I find it easier for you to do this by the application itself, through a Middleware (if using Laravel 5).

The code comes down to this:

  if (! Request::secure()) {
         return Redirect::secure(Request::path());
  }
  • Does this make 301 redirect? Missing also remove www.

  • Just follow the logic, in 12 minutes, on my lunch break, I’ll slap the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.