简体   繁体   English

使用htaccess将所有子域从HTTP重定向到HTTPS

[英]Redirect all subdomains from HTTP to HTTPS using htaccess

I have domain and its working with https for example 我有域,例如使用https

http://example.com/

Now I am creating sub domains like demo1.example.com, demo2.example.com and so on. 现在,我正在创建子域,例如demo1.example.com,demo2.example.com等。

I need all sub domains must be redirect and access by only https. 我需要所有子域都必须重定向,并且只能通过https访问。

I have write the code in my .htaccess see below but its always redirecting to the main domain when I access my sub domains. 我已经在下面的.htaccess中编写了代码,但是当我访问自己的子域时,它始终重定向到主域。

My htaccess code is 我的htaccess代码是

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

EDIT 编辑

I have main website at root and all other sub-domains code at one of folder. 我的主网站位于根目录,所有其他子域的代码位于文件夹之一。 So whenever any sub-domain created that will point to that folder only. 因此,无论何时创建的任何子域都将仅指向该文件夹。

The following worked for me. 以下对我有用。 NOTE: if your subdomain has its own .htaccess file (for example a WordPress site in your subdomain), you will have to edit that .htaccess file in addition to the main, domain .htaccess. 注意:如果您的子域具有自己的.htaccess文件(例如,您的子域中的WordPress站点),则除了主域.htaccess外,您还必须编辑该.htaccess文件。

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

You can add other rewrite conditions above or below this, depending on when you want them executed. 您可以在此之上或之下添加其他重写条件,具体取决于您希望它们执行的时间。 For example, Reghbendra Nayak's "Handle Front Controller..." code can be placed blow the https 'force' and it will execute. 例如,可以通过https'force'放置Reghbendra Nayak的“ Handle Front Controller ...”代码,它将执行。

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM