dongwubao4785 2014-09-17 17:31
浏览 77
已采纳

.htaccess:重定向到HTTPS主域

I have this really weird problem which I've already spent a lot of time investigating, but failed to find a solution.

Basically, I have a few domains parked in the server, but for simplicity, let's assume just 2 domains:

  1. example.co.nz (main)
  2. example.com

I have 2 requirements I'd like to satisfy:

  • a) I want all traffic from (2) redirected to (1)
  • b) I want all HTTP traffic to be redirected to HTTPS

So basically, in one sentence, I want all traffic to simply go to https://www.example.co.nz no matter what URL patterns I type into the location bar.


I have written a .htaccess file in the root like this:

SetEnv TZ

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTPS} !^on [OR]
    RewriteCond %{HTTP_HOST} !^www\.example\.co\.nz
    RewriteRule (.*) https://www.example.co.nz/$1 [R=301,L]

    RewriteCond %{REQUEST_URI} ^/(system|lib)(.*)$
    RewriteRule ^(.*)$ index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

The following works:

The following does not get redirected:


  • Would anyone happen to have any ideas why this is happening?

I was wondering if this is related to my htaccess or the server itself.

Any help would be high appreciated.

  • 写回答

1条回答 默认 最新

  • doutun1875 2014-09-17 17:37
    关注

    Found the problem.

    The redirect is actually working. You get a warning about the certificate at https://www.example.com when you attempt to visit the website. Once you accept the warning, you are redirected to https://www.example.co.nz/.

    That is because the SSL check is applied before the .htaccess rewrites. Since there is no certificate installed for www.example.com, you get a warning.

    The only way around that is to install an SSL certificate for www.example.com as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题