集成 .htaccess 代码以将所有子域从 http 重定向到 https 到 Wordpress .htaccess

integrating .htaccess code to redirect all subdomains from http to https into Wordpress .htaccess

this answer 开始,我获得了 .htaccess 将子域从 http 重写为 https 的规则:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteRule ^ https://%1.example.com%{REQUEST_URI} [NE,L,R]

但是,将它们添加到我的 .htaccess 并没有成功地将我的 Wordpress MU 子域站点从 http://*.example.com.au 重定向到 https://*.example.com.au

这是我的 .htaccess:

### Caching ###

# WPSuperCache
...
# END WPSuperCache

### URL Rewriting ###

# example.com.au > www.example.com.au #

<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^example.com.au
RewriteRule (.*) http://www.example.com.au/ [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
</IfModule>

# http://*.example.com.au > https://*.example.com.au

<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example.\.com\.au$
RewriteRule ^ https://%1.example.com.au%{REQUEST_URI} [NE,L,R]
</IfModule>

# General redirects and rewrites

Redirect 301 ...

<IfModule mod_rewrite.c>
...nothing related to example.com.au or *.example.com.au
</IfModule>

### Wordfence ###

# BEGIN litespeed noabort
<IfModule rewrite_module>
        RewriteEngine On
        RewriteRule .* - [E=noabort:1]
</IfModule>
# END litespeed noabort

### Wordpress ###

# Begin Wordpress
<IfModule rewrite_module>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file= [L]

# add a trailing slash to /wp-admin

RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)  [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$  [L]
RewriteRule . index.php [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file= [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]

# END WordPress

### MainWP ###

感谢帮助。

您的正则表达式在 example 之后有一个额外的 DOT,它将在此处匹配后面的单个字符:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example.\.com\.au$
RewriteRule ^ https://%1.example.com.au%{REQUEST_URI} [NE,L,R=301]

使用以下方法修复它:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com\.au$ [NC]
RewriteRule ^ https://%1.example.com.au%{REQUEST_URI} [NE,L,R]