将https://*.example.com重定向到https://example.com

时间:2015-08-21 21:56:09

标签: regex apache .htaccess redirect ssl

请求:

我的证书不允许使用通配符,仅适用于https://example.com

我想重定向所有流量:http(s)://*.example.com - > https://example.com

问题:

http://*.example.com/*https://example.com/*

可以正常使用

我从以下地址收到错误 NET :: ERR_CERT_COMMON_NAME_INVALID

https://*.example.com/*https://example.com

到目前为止我尝试了什么......

这是虚拟主机的default.conf配置文件:

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

<IfModule mod_ssl.c>
<VirtualHost *:443>
        NameVirtualHost *:443
        ServerName example.com
        ServerAlias *.example.com
        Redirect permanent "(?i)https:\/\/\d*[a-z0-9]*.example.com" "https://example.com"

我已经尝试了上面最后一个重定向行的几种变体,正如几个相关问题所示。我现在只能发布两个,因为我是新来的:

我猜这是行不通的,因为服务器在处理重定向代码之前会返回证书错误。我上面提到的一个问题暗示了这一点,但它并没有被接受作为答案而且没有人真正证实或否认它。

我的问题(最后!):

是否可以在没有通配符证书的情况下执行此操作?如果是这样,我上面的Redirect代码出了什么问题?据我所知,正则表达式正在处理从https://*.example.comhttps://example.com的不区分大小写的重定向。

2 个答案:

答案 0 :(得分:3)

  

是否可以在没有通配符证书的情况下执行此操作?

没有。原因是因为您有一个证书表明唯一有效的主机名是 example.com ,并且首先发生的是与Web服务器建立连接并建立SSL握手。此握手在任何HTTP通信发生之前发生。这意味着,在握手期间,浏览器会获得证书,其中显示“您应该只与 example.com 交谈”,浏览器会看到它只是尝试连接到“ www .example.com “,浏览器就在那里停止并返回您看到的异常。此异常不是来自服务器,而是浏览器说某些内容不正确。

由于所有这些都发生在甚至进行任何HTTP通信之前(因为它必须加密,并且在握手建立之前无法加密),因此无法做任何事情至于mod_rewrite或从apache重定向,因为Apache还没有收到请求。

答案 1 :(得分:0)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

在.htaccess文件中添加以上代码 您的网址将重定向,如: https://www.example.com/test至 - &gt; https://example.com/test

相关问题