简体   繁体   中英

htaccess redirect all subdomains to https except www

I'd like to redirect all subdomains (i've wildcard subdomains set up in apache virtualhost) to https, if http, for all except www.domain.com

Any ideas greatfully appreciated.

Here's what I have so far:

# Redirect subdomains to https
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^(*)\. [NC]
#RewriteCond %{HTTP_HOST} !^(www)\. [NC]
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Thanks!

Try this code in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This should help you:

RewriteCond %{HTTP_HOST} !^(www\.)localhost.com$
RewriteCond %{HTTP_HOST} ^(.*?)\.localhost.com$
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*?)$ https://%{HTTP_HOST} [nc]

First RewriteCond : If the website doesn't start with www

Second RewriteCond : Get any subdomain

Third RewriteCond : Check that https isn't already within the requested url

RewriteRule : Redirect to https version of site

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