简体   繁体   中英

Redirect to https://www. when there is no subdomain and just https: when there is

When someone enters on http:// (non-secure), there are two possible scenarios. I'd like to program an .htaccess file to account for both in the following ways:

1)They do not use a subdomain.

Solution: redirect to https://www. + remaining URL.

2) They use either http://www. or http://subdomain.

Solution: redirect to https://subdomain OR https://www. (whichever used) + remaining URL.

How can I solve this within my .htaccess file?

Here is my current file:

 RewriteEngine on
 RewriteCond $1 !^(index\.php|images|css|js|font|uploads|robots\.txt)
 RewriteRule ^(.*)$ /index.php/$1 [L]

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# redirect to https://www. + remaining URL.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# They use either http://www. or http://subdomain.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [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