.htaccess在预测访问子域时出错

.htaccess在预测访问子域时出错,.htaccess,.htaccess,我正在使用example.com的当前代码 RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC,OR] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE] 这使得您添加了fortully或https://如果您已经放置了www 这段代码的

我正在使用example.com的当前代码

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
这使得您添加了fortully或https://如果您已经放置了www

这段代码的问题是,即使我调用诸如ex.example.com这样的子域

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
它补充道

我想要的是子域像这样重定向=>


有谁能给我提供一个有效的代码吗?

希望这可以正常工作

RewriteEngine On

#for normal domains like example.com or example.in to redirect to https://www.example.com
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^([^\.]+)\.(com|in)$ [NC]
RewriteRule ^ https://www.%1.%2%{REQUEST_URI} [R=301,L,NE]

#for subdomain like test.example.com to redirect to https://test.example.com
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^([\.\w\-]*)\.(com|in)$ [NC]
RewriteRule ^ https://%1.%2%{REQUEST_URI} [R=301,L,NE]

希望这会很好

RewriteEngine On

#for normal domains like example.com or example.in to redirect to https://www.example.com
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^([^\.]+)\.(com|in)$ [NC]
RewriteRule ^ https://www.%1.%2%{REQUEST_URI} [R=301,L,NE]

#for subdomain like test.example.com to redirect to https://test.example.com
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^([\.\w\-]*)\.(com|in)$ [NC]
RewriteRule ^ https://%1.%2%{REQUEST_URI} [R=301,L,NE]

要强制对子域使用https,请将以下内容放在行的重写引擎之后:

RewriteCond %{HTTPS}#%{HTTP_HOST} ^off#sub\.example\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

这会将重定向到。您已将此项置于其他HTTPS重定向规则之前,否则您的规则将覆盖此项。

若要强制子域使用HTTPS,请将以下内容置于重写引擎后的第行:

RewriteCond %{HTTPS}#%{HTTP_HOST} ^off#sub\.example\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

这会将重定向到。您已将其置于其他HTTPS重定向规则之前,否则您的规则将覆盖此规则。

为什么为PHP标记此规则?这与PHP无关。是否要将此
example.com
转换为
https://www.example.com
及其子域到
http://that.subdomain.com
?我想要类似
https://that.example.com
而不是
http
@AkshayShrivastav检查它是否工作正常?为什么要将其标记为PHP?这与PHP无关。是否要将此
example.com
转换为
https://www.example.com
及其子域到
http://that.subdomain.com
?我想要类似
https://that.example.com
代替
http
@AkshayShrivastav检查它是否正常工作?它主要用于域,根据子域,它唯一为mail.test.com工作的不是demo.test.com,为什么?它也应该为这个工作,让我们再检查一次它为主域工作,根据子域,它唯一为mail.test.com工作的不是demo.test.com,为什么?它也应该为那个工作,让我们再检查一次