Apache - 如何仅将http请求转换为https?

时间:2016-05-04 15:27:27

标签: apache .htaccess zend-framework2 centos httpd.conf

如何判断用户是否使用http://ttt.comhttp://www.ttt.com,将其重定向到https://www.ttt.com

的httpd.conf:

<VirtualHost *:80>
 ServerName www.ttt.com
 ServerAlias ttt.com
 DocumentRoot /home/www/html/ttt/public
 <Directory /home/www/html/ttt/public>
    #Options ExecCGI
    #AddDefaultCharset utf-8
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all
 </Directory>
</VirtualHost>

.htaccess:

RewriteEngine On
############################################
## always send 404 on missing files in these folders
#RewriteCond %{REQUEST_URI} !^/(files)/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

3 个答案:

答案 0 :(得分:3)

在主目录.htaccess文件中尝试以下代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

答案 1 :(得分:2)

在这里:https://wiki.apache.org/httpd/RedirectSSL

  

将请求重定向到SSL

     

我们假设您希望始终发送http://www.example.com/secure/   通过SSL(我在这里假设普通和SSL vhost都有   相同的内容)。您可以通过链接到正确的页面来完成此操作   从您的HTML页面中...但总会有一些用户   会那样偷偷摸摸。

     

使用虚拟主机(使用重定向)

     

使用SSL时,您经常会拥有至少两个虚拟主机:   一个在端口80上,用于处理普通请求,另一个在端口443上   提供SSL。如果您希望将用户从非安全站点重定向到   在SSL站点中,您可以使用普通的Redirect指令   非安全的VirtualHost:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   Redirect permanent /secure https://mysite.example.com/secure
</VirtualHost>

<VirtualHost _default_:443>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>
     

重定向所有内容时,您甚至不需要DocumentRoot:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent / https://secure.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName secure.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>
     

注意:重定向也可以在.htaccess文件或地址中使用   特定的URL,如:

     

示例:

     
    

重定向永久/登录https://mysite.example.com/login

  
     

使用mod_rewrite

     

虽然推荐使用该解决方案,因为它更简单   而且更安全,你也可以使用mod_rewrite来获得相同的效果   这里描述:RewriteHTTPToHTTPS

来自https://httpd.apache.org/docs/trunk/mod/mod_alias.html#redirect

# Redirect to a URL on a different host Redirect "/service" "http://foo2.example.com/service"

# Redirect to a URL on the same host Redirect "/one" "/two"
     

如果客户请求http://example.com/service/foo.txt,它将会是。{   告诉他访问http://foo2.example.com/service/foo.txt。这个   包括具有GET参数的请求,例如   http://example.com/service/foo.pl?q=23&a=42,它会被重定向到   http://foo2.example.com/service/foo.pl?q=23&a=42。请注意,POST会   被丢弃只有完整的路径段匹配,所以上面   示例与请求不匹配   http://example.com/servicefoo.txt。对于使用的更复杂的匹配   表达式语法,省略URL-path参数,如下所述。   或者,要使用正则表达式进行匹配,请参阅   RedirectMatch指令。

如果您希望重定向www.example.com/*example.com/*,可以使用不同的ServerName创建两个VirtualHost,也可以使用Rewrite插件。

答案 2 :(得分:2)

您可以使用以下命令从httpd.conf中执行此操作:

<VirtualHost *:80>
    ServerName www.ttt.com
    Redirect "/" "https://www.ttt.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.ttt.com
    ...
    ...
</VirtualHost>

或者来自.htaccess文件:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]