Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 将任何子域重写为https_Apache_Https - Fatal编程技术网

Apache 将任何子域重写为https

Apache 将任何子域重写为https,apache,https,Apache,Https,目前,我正在LAMP系统上运行一个带有apache的小型web服务器。我已经为我的域创建了https证书。现在我想将任何子域和子域目录重写为不带www的https。我的.htaccess文件中已经有一个代码,它将www版本重写为非www和https 以下是my.htaccess文件(位于根apache目录中)中的代码: 我已经尝试将“www”与“(*)”交换,但我不确定“%1”和“$1”的作用,因为我在internet上找到了该代码,但他们没有解释。我研究过正则表达式,但什么也找不到 Rewri

目前,我正在LAMP系统上运行一个带有apache的小型web服务器。我已经为我的域创建了https证书。现在我想将任何子域和子域目录重写为不带www的https。我的.htaccess文件中已经有一个代码,它将www版本重写为非www和https

以下是my.htaccess文件(位于根apache目录中)中的代码:

我已经尝试将“www”与“(*)”交换,但我不确定“%1”和“$1”的作用,因为我在internet上找到了该代码,但他们没有解释。我研究过正则表达式,但什么也找不到

RewriteEngine On
RewriteBase /

# domain starts with www (change yourdomain to your domain name)
# adding the domain name will ensure its not trying to capture a subdomain with www
# For example: ^www\.(.*)$ will redirect www.subdomain.domain.com to https.
RewriteCond %{HTTP_HOST} ^www\.(yourdomain) [NC]
# then redirect to HTTPS
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]

# check if HTTPS is not being used
RewriteCond %{HTTPS} off
# and because we already redirect www. we exclude it
RewriteCond %{HTTP_HOST} !^www\. [NC]
# and now we test to see if its a subdomain (change yourdomain to your domain name)
RewriteCond %{HTTP_HOST} ^[^\.]+\.yourdomain [NC]
# now we finally redirect it
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
确认子域重定向工作后,将
R=302
更改为
R=301
,其原因是为了避免浏览器缓存重定向,直到您能够确保其工作为止

如果您在使用
R=301
时出错,则重定向将被缓存,并且在清除错误或使用其他浏览器之前,浏览结果可能不可靠

%1
用于从
RewriteCond
捕获内容,而
$1
则从
RewriteRule
捕获

如果您有任何结果导致多个值,则该数字表示捕获的值索引,如下面使用
$1
$2
所示


使用
#
进行注释时,请确保注释始终位于新行中,而不是在规则末尾,否则将无法工作。

我需要在哪里找到子域重定向的代码?如果Apache文件夹?@JohnMamor也在根目录中,这将取决于您如何创建这些子域,如果它们指向主域所在的同一根文件夹,则可以将这些规则放在同一文件中。否则,您必须在子域所在的位置内创建一个新的
.htaccess
。子域不是我自己创建的。我为我的域名买了一封网络邮件,它在我的主机上创建了DNS记录。“我的服务器”上没有子域的位置。您需要在apache中设置通配符或配置自己的子域,否则apache将对在其上配置的服务器的默认ip/域执行全面捕获。因此,如果没有关于您所在域的服务器的进一步技术信息,将很难为您提供帮助,在这一点上,这不再是一个.htaccess问题,而是另一个关于在DNS配置完成后如何将这些子域分配给服务器的问题。@JohnMamor不客气。如果您发布了另一个关于在apache上配置域的问题,以及关于如何托管域的信息(如果是您的服务器、另一家公司、如果您有控制面板等),请将其链接到此处,如果我知道我会帮您解决。
RewriteEngine On
RewriteBase /

# domain starts with www (change yourdomain to your domain name)
# adding the domain name will ensure its not trying to capture a subdomain with www
# For example: ^www\.(.*)$ will redirect www.subdomain.domain.com to https.
RewriteCond %{HTTP_HOST} ^www\.(yourdomain) [NC]
# then redirect to HTTPS
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]

# check if HTTPS is not being used
RewriteCond %{HTTPS} off
# and because we already redirect www. we exclude it
RewriteCond %{HTTP_HOST} !^www\. [NC]
# and now we test to see if its a subdomain (change yourdomain to your domain name)
RewriteCond %{HTTP_HOST} ^[^\.]+\.yourdomain [NC]
# now we finally redirect it
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]