简体   繁体   中英

Force http to https via htaccess file, not working

So my issue is if I visit my website it is unsecured. However, if I put https://www.websitename.co.uk it will display the website with https and secure bar. However, when I upload my htaccess file the website just doesn't want to connect.

使用htaccess时出现错误信息

If I comment out the code below from the htaccess file, the website works but doesn't show secure. If I add that code it just gives me the error message as seen in the image.

 # Redirect http to https editor nathan 18/01/2017
    #RewriteEngine On
    #RewriteCond %{HTTPS} off
    #RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess File

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

# Redirect http to https editor nathan 18/01/2017
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect non www. to www.
RewriteCond %{HTTP_HOST} !^www\.MYWEBSITE\.co\.uk
RewriteRule (.*) http://www.MYWEBSITE.co.uk/$1 [R=301,L]

# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1? [R=301,L]

# Remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^(.+)\.php /$1 [R=301,L,QSA]

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

Please change this line:

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

Into this:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This means if the visitor go to http (which is port 80) will get redirect to https (which is 443)

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