301 htaccess redirect with and without www to HTTPS

Asked

Viewed 91 times

2

Good morning. I’d like your help.

I need to make an htaccess that works like this:

  • Redirect 301: "site.com" or "www.site.com" for https://www.site.com
  • If the person comes from a google url, and does not have https, force the https entry.

Thank you.

1 answer

1

To redirect all traffic to Https just add this line in your .htaccess

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{SERVER_PORT} !^443$
   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Where the domain.com.br is your website URL, this is the fastest way I know how.

To do only redirect with www for www-free add these lines into your .htaccess:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^www.google.com$
   RewriteRule (.*) http://google.com/$1 [R=301,L]
</IfModule>

To redirect from without www towards www add these lines into your .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^dominio.com
    RewriteRule ^ https://www.dominio.com%{REQUEST_URI} [L,R=301]
</IfModule>

Trade the.com domain for your domain,

OBS: remember to exchange the https for http if the site does not have ssl

In case I know for example, if I wanted to redirect the whole site to https and without www would be:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$    
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} ^www.dominio.com.br$
    RewriteRule (.*) https://dominio.com.br/$1 [R=301,L]
</IfModule>

I redirect to https, and after that I remove www. from your dominio.com.br, reminding you to switch to your domain in question.

I ended up finding a site very practical: https://www.aleydasolis.com/htaccess-redirects-generator/https-vs-http/ just select what and generate.

  • But that doesn’t check whether this with or without www

  • For redirect pair with www and one form and for without www and another I will update with both options;

Browser other questions tagged

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