子域上的http重定向到主域;子域名可与https一起使用

时间:2018-09-19 11:45:30

标签: wordpress .htaccess ssl subdomain url-redirection

我有一个根域https://example.com和子域https://sub.example.com,并且在根域example.com上具有肯定SSL

我的问题是,每当我尝试访问http://sub.example.com时,我都会正确地重定向到https://example.com,但是当我访问https://sub.example.com时,却没有重定向。如何将https://sub.example.com重定向到https://example.com

.htaccess在子域中:

# BEGIN WordPress
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 6 hours"
    ExpiresByType image/jpeg "access plus 6 hours"
    ExpiresByType image/gif "access plus 6 hours"
    ExpiresByType image/png "access plus 6 hours"
    ExpiresByType text/css "access plus 6 hours"
    ExpiresByType application/pdf "access plus 1 week"
    ExpiresByType text/javascript "access plus 6 hours"
    ExpiresByType text/html "access plus 10 minutes"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 3 hours"
</IfModule>
Header set X-Endurance-Cache-Level "2"

# END WordPress

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com$
    RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
    RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
</IfModule>

任何帮助或建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的条件只查找HTTP请求

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

尝试通过HTTPS添加相同的规则

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