【问题标题】:Redirect ALL traffic to https including subdomains and remove www将所有流量重定向到 https,包括子域并删除 www
【发布时间】:2016-03-11 06:36:07
【问题描述】:

我正在尝试使用以下规则创建一个 .htaccess 文件:

http://www.example.com/test => https://example.com/test
http://test.example.com/test => https://test.example.com/test
https://www.example.com/test => https://example.com/test

这是我当前的 .htaccess 文件:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.nebla.co.uk$ [NC]
RewriteRule ^(.*)$ http://nebla.co.uk/$1 [R=301,L]

RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

但是这会导致一些奇怪的行为:

http://www.nebla.co.uk/ => https://nebla.co.uk/public_html

它还会导致子域上的重定向循环。

【问题讨论】:

  • RewriteRule 前面加上RewriteCond HTTPS off 怎么样?允许重定向的配置是什么? (目标部分带有public_html 的那个?您是否尝试阅读文档?例如httpd.apache.org/docs/2.4/mod/mod_rewrite.html
  • 我确实已经阅读了文档并寻求帮助,但是似乎每个人都希望主站点转到 https 而所有子域都转到 http,我不希望这种行为。我已经更新了我的问题。
  • 这些规则中没有public_html。任何可能在“主”配置文件中定义的东西(通常httpd.conf,可能在/etc/httpd 或*nix 服务器的'/etc/apache2` 中,不确定它们在哪里用于其他操作系统。(哦,和顺便说一句,我在测试时没有重定向到public_html
  • 不幸的是,我无法访问任何配置文件,因为我正在使用托管服务,我想这会使问题更加“为什么要添加 public_html””

标签: apache .htaccess http https


【解决方案1】:

我设法改正了我的行为,而不是使用

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.nebla.co.uk$ [NC]
RewriteRule ^(.*)$ http://nebla.co.uk/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

我把 $1 改成 %{REQUEST_URI} 喜欢:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.nebla.co.uk$ [NC]
RewriteRule ^(.*)$ http://nebla.co.uk/%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2019-06-14
    • 2020-05-16
    • 2014-11-12
    • 2017-04-22
    相关资源
    最近更新 更多