php - 在不破坏单页 ssl 的情况下重写子域

标签 php apache .htaccess mod-rewrite ssl

是否可以用具有单页 ssl 证书的域重写子域?

我想做什么:

Rewrite subdomain.domain.com to domain.com/index.php&page=subdomain

到目前为止我得到了什么:

   RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ /index.php?user=%1&req=$1 [L,QSA]

这给了我一个错误的 SSL 证书,因为我没有子域证书。 在 php 中对 $_SERVER 使用 var_dump 返回以下内容:

["SCRIPT_URI"]=> string(24) "https://subdomain.domain.com/" 
["HTTPS"]=> string(2) "on"  
["SSL_TLS_SNI"]=> string(15) "subdomain.domain.com"  
["SSL_SERVER_S_DN_CN"]=> string(14) "www.domain.com"

所以它仍然将它作为子域传递而不是重定向它。

如果我使用完整域作为重写规则,它可以工作,但它也会将标题栏中的 url 更改为 domain.com/index.php?user=subdomain,而不是保留 url:

RewriteRule ^(.*)$ https://domain.com/index.php?user=%1&req=$1 [L,QSA]

那么如何在保留 subdomain.domain.com 的同时将子域重写为 domain.com/index.php?user=subdomain 并且仍然能够使用一页 ssl 证书呢?

最佳答案

不可能让网络服务器在子域(例如 subdomain.domain.com)上提供网页,而是保留仅在 domain.com< 上有效的单页 SSL 证书是不可能的。从技术上讲这是可能的,但这样做会使浏览器警告用户 SSL 证书无效,并使访问者对您的网站产生不信任。现在有几个选项可以解决这个问题:

  1. 获取通配符 SSL 证书
  2. 获取多主题备用名称 SSL 证书(旧版/移动浏览器可能会警告用户此类证书)
  3. 制作一个新的 apache 虚拟主机配置,让 apache 提供不同的单页 SSL 证书(您需要为 subdomain.domain.com 获取)

这里最好的选择是 3,因为您可以在多个网站上获得免费的一页 SSL 证书并将它们用于您的目的(例如 StartSSL、LetsEncrypt)。设置新的虚拟主机配置并不难,因为您可以复制粘贴网站现有虚拟主机配置的大部分(您只需要更改一些内容(例如 SSL 证书、SSL key 、SSL 链、站点名称) ...)).

关于php - 在不破坏单页 ssl 的情况下重写子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36166095/

相关文章:

php - 将 laravel 站点移至 Centos 服务器时找不到类 'Config'

php - 将 Angular 4 与 ngserve 和 php 项目集成到现有的 apache 实例中

Node.js 从 URL 中删除端口

java - 为 www.example.com 而不是 example.com 设置 cookie

regex - 将查询字符串重写为另一个查询字符串

.htaccess - https 和 www 重定向的问题

php - 你如何为 LAMP 项目实现 "one step build"?

php发现总日期时间差异(以秒为单位)未达到预期

php - 无法访问特征中的 protected 属性

php - 确保重写的 URL 总能找到您期望的内容的正确方法?