简体   繁体   中英

.htaccess redirect http > https with 301 redirects

I have recently had an SSL certificate installed and now my entire website uses it, without errors. I had set up a redirect from http to https but realised it was using a 302 redirect. I currently use the following code which uses a 301 redirect.

Is this correct? Can I remove any part of this, or should I change any part of this?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]


RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Thank you for checking.

The first part of your code is weird, why you redirect on HTTP all and after that on https in second part :) ?

Here is my example, it is similar to yours but I don't need to change every time domain name for example if you use the same pattern on different servers + this will redirect all traffic from http to https

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/\d+.BIN_AUTOSSL_CHECK_PL__.\w+.tmp$ [NC]
RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+..+.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

The RewriteCond %{HTTPS} on portion may not work for all web servers. My webhost requires RewriteCond %{HTTP:X-Forwarded-SSL} on, for instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM