在没有 ServerName 的默认虚拟主机上将 HTTP 重定向到 HTTPS [英] Redirect HTTP to HTTPS on default virtual host without ServerName

查看:22
本文介绍了在没有 ServerName 的默认虚拟主机上将 HTTP 重定向到 HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 apache 服务器上,我希望能够将所有传入的 http 请求重定向到等效的 https 请求.问题是我希望能够在不指定 ServerName 的情况下为我的默认虚拟主机执行此操作,并使重定向与请求 url 中出现的任何服务器名称一起工作.我希望有这样的事情:

On my apache server I'd like to be able to redirect all incoming http requests to the equivalent https request. The catch is that I'd like to be able to do this for my default virtual host without specifying the ServerName and have the redirect work with whatever server name appeared in the request url. I'm hoping for something like this:

NameVirtualHost *:80
<VirtualHost *:80>
    RedirectPermanent / https://%{SERVER_NAME}/
    ...
</VirtualHost>

这可以使用 Redirect 还是我必须求助于 Rewrite?

Is this possible using Redirect or will I have to resort to Rewrite?

推荐答案

尝试将其添加到您的 vhost 配置中:

Try adding this in your vhost config:

RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]

这篇关于在没有 ServerName 的默认虚拟主机上将 HTTP 重定向到 HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆