【问题标题】:force https on subdirectory and redirect non-www traffic to www在子目录上强制 https 并将非 www 流量重定向到 www
【发布时间】:2014-02-20 08:59:38
【问题描述】:

所以在过去的几个小时里,我一直对我的 .htaccess 文件无法正常工作感到沮丧。

我有两个子目录:secure.mysite.ca 和 storage.mysite.ca,它们都需要进行 ssl 加密。当我将 https 放在 URL 前面(即 https://secure.mysite.ca/https://storage.mysite.ca/)时,这些站点可以工作,但是当我将 http 放在前面时,它会重定向到我的根网站(http://mysite.ca/)。当有人尝试访问未加密版本时,我想重定向到站点的 https 版本。

我还想将所有访问 http://mysite.ca/ 的流量重定向到 http://www.mysite.ca/(即前面的 www)

这个 .htaccess 文件一年多以前可以工作,但现在不行了。

这是我在根文件夹中找到的 htaccess 文件:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.ca$ [NC]
RewriteRule ^(.*)$ http://www.mysite.ca%{REQUEST_URI} [R=permanent,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} ^(secure|storage)\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]  

我做错了吗?我应该在子目录中放置一个 .htaccess 文件吗?

编辑:这是 secure.fixnode.ca、storage.fixnode.ca 和 www.fixnode.ca 的虚拟主机配置文件。请让我知道我做错了什么

storage.fixnode.ca.conf

<VirtualHost *:443>
DocumentRoot "/var/www/fixnode_website/content/Online Storage"
<Directory "/var/www/fixnode_website/content/Online Storage">
allow from all
Options +Indexes
</Directory>
SSLEngine on
ServerName storage.fixnode.ca
SSLCertificateFile /path/to/cert/storage.crt
SSLCertificateKeyFile /path/to/private key/private.key
</VirtualHost>

secure.fixnode.ca.conf

<VirtualHost *:443>
DocumentRoot "/var/www/fixnode_website/content/Secure Login"
<Directory "/var/www/fixnode_website/content/Secure Login">
allow from all
Options +FollowSymlinks
</Directory>
SSLEngine on
ServerName secure.fixnode.ca
SSLCertificateFile /path/to/cert/cert.crt
SSLCertificateKeyFile /path/to/cert/mykey.key
</VirtualHost>

最后是我的根域 www.fixnode.ca.conf 的虚拟主机

<VirtualHost *:80>
DocumentRoot /var/www/fixnode_website
<Directory "/var/www/fixnode_website">
allow from all
Options +Indexes
</Directory>
ServerAlias fixnode.ca
ServerName www.fixnode.ca
</VirtualHost>

【问题讨论】:

  • 你是说上面的 htaccess 代码以前有效,现在无效?
  • 是的。目前访问secure.fixnode.ca的用户只有在手动将https://放在URL前面时才会被重定向到正确的站点。否则,如果用户只是访问 secure.fixnode.ca 而没有在前面输入 https:// 那么该用户将被重定向到我的根站点(fixnode.ca)
  • 除非您对系统进行更改,否则通常不会更改。它应该会继续工作。
  • 我最近更新到 apache 2.4。我认为更新破坏了我的 .htaccess 文件
  • 是的,您可能在某处覆盖了 httpd.conf 文件并破坏了虚拟主机。

标签: .htaccess mod-rewrite redirect https apache2


【解决方案1】:

如果您最近对系统进行了更改,您可能已经覆盖了您的 apache 配置文件(例如 httpd.conf or apache2.conf),这可能会将选项重置为默认值。

【讨论】:

    【解决方案2】:

    您需要将此规则放在两个子域的两个子目录中:

    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(secure|storage)\. [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    【讨论】:

    • 问题不在子目录内。问题是将流量从根目录重定向到子目录。目前,如果我访问secure.fixnode.ca(我的网站),它会转到与www.fixnode.ca 相同的站点。这意味着我的子目录首先没有转发到正确的文档根目录。
    猜你喜欢
    • 1970-01-01
    • 2018-08-20
    • 2017-03-30
    • 2016-09-05
    • 2014-02-23
    • 2013-09-18
    • 2014-05-12
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多