Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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 使用.htaccess从http重定向到https_Apache_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Apache 使用.htaccess从http重定向到https

Apache 使用.htaccess从http重定向到https,apache,.htaccess,mod-rewrite,redirect,Apache,.htaccess,Mod Rewrite,Redirect,我在同一台服务器上有两个域。一个域用于生产,另一个域用于在将内容复制到生产之前测试内容。我试图创建一个重写规则,将http重定向到https。我如何写它,使域名不是硬编码?我需要它的原因是,当我将测试内容复制到生产服务器时,我不想更改.htaccess文件。这是我的东西,但它不起作用 # Prevent SSL cert warnings <IfModule mod_rewrite.c> RewriteCond %{SERVER_PORT} !^443 RewriteRu

我在同一台服务器上有两个域。一个域用于生产,另一个域用于在将内容复制到生产之前测试内容。我试图创建一个重写规则,将http重定向到https。我如何写它,使域名不是硬编码?我需要它的原因是,当我将测试内容复制到生产服务器时,我不想更改.htaccess文件。这是我的东西,但它不起作用

 # Prevent SSL cert warnings
 <IfModule mod_rewrite.c>
  RewriteCond %{SERVER_PORT} !^443
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 </IfModule>
#防止SSL证书警告
重写cond%{SERVER\u PORT}^443
重写规则^https://%{HTTP_HOST}%{REQUEST_URI}[R=301,L]

您的尝试非常接近

您可以尝试以下规则:

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

为了澄清,您是填写服务器名称还是将其保留为书面形式?不理解您的问题。正如您所见,我在回答中没有使用
SERVER\u NAME
而只是使用
HTTP\u HOST
对不起,您当时是在填写HTTP\u主机还是将其保持为书面形式?问题是关于
HTTP->https
的,所以
HTTP\u HOST
被带到https URLOk,所以您不需要用www.example.com替换HTTP\u主机?
RewriteEngine On

# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTPS} !=on


RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]