简体   繁体   中英

Redirect 301 from http to https doesn't work

I've this .htaccess in my share web hosting.

RewriteOptions inherit
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]


RedirectMatch 301 ^/directory(.*)$ https://www.example.com/$1
RedirectMatch 301 ^/directory/(.*)$ https://www.example.com/$1
RedirectMatch 301 ^/directory\-2014\.03\.04(.*)$ https://www.example.com/$1
RedirectMatch 301 ^/directory\-2014\.03\.04/(.*)$ https://www.example.com/$1

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]

I want all calls to http://www.example.com or http://example.com that redirect to https://www.example.com . But with my setup does not work either.

Try replacing:

RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "https\:\/\/www\.domain\.com\/" [R=301,L]

with

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

The above will first change http://domain.com to http://www.domain.com then it will redirect http://www.domain.com to https://www.domain.com if it isn't https://

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