简体   繁体   中英

.htaccess non-www to www and http to https

I know this question has been asked and answered many times but I've spent the last 3 hours going through peoples answers and none have seemed to work just right for me.

I need some .htaccess code to do this:

If domain is example.co.uk/$urlData (only apply to main domain and no subdomains) rewrite too www.example.co.uk/$urlData

If HTTPS is not "on" and domain is primary (ie. no subdomains) then redirect to https://www.example.co.uk/$urlData .

I have to use RewriteCond %{HTTP:X-Forwarded-Proto} !https to test if HTTPS is off as RewriteCond %{HTTPS} off is not configured on my server.

Give this a try and see if this will work for you.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]

Add this to your .htaccess :

RewriteEngine On

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

RewriteCond %{HTTP_HOST} ^www\.example\.co\uk$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]

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