使用apache&将www重定向到非www https的.htaccess

时间:2014-09-04 00:10:36

标签: apache .htaccess redirect web

我已经看到很多这些问题被问到了,但我还没有找到适用于我的情景的问题。我几乎尝试了每一个,但却无法让它发挥作用。

有人可以解释如何在没有www的情况下让www.domain.comwww.subdomain.domain.com重定向到https吗?

这是我到目前为止所做的:

目前,我有以下DNS记录:

A         @                1.2.3.4
A         subdomain        1.2.3.4
CNAME     www              domain.com.
CNAME     www.subdomain    subdomain.domain.com.

我还有一个虚拟主机文件,如下所示(几乎完全是子域的副本)

<VirtualHost *:80>
     RewriteEngine on
     ReWriteCond %{SERVER_PORT} !^443$
     RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin webmaster@localhost
    DocumentRoot /path/to/public_html

    SSLEngine on
    SSLCertificateFile /path/to/domain.crt
    SSLCertificateKeyFile /path/to/domain.key
    SSLCertificateChainFile /path/to/domain-bundle

    <Directory "/path/to/public_html">
       AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/domain-error.log
    CustomLog ${APACHE_LOG_DIR}/domain-access.log combined
</VirtualHost>

2 个答案:

答案 0 :(得分:0)

在443虚拟主机中,尝试添加:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

答案 1 :(得分:0)

所以这就是我所学到的。 www.domain.comdomain.com不同。这也包括子域名。理想情况下,您希望获得这两个名称的SSL认证。但是,如果你像我一样,我真的不想花这笔钱。

如果用户在地址栏中输入以下内容,则以下修复工作正常:

http://www.domain.com   => Will be redirected to https://domain.com
www.domain.com          => Will be redirected to https://domain.com
https://domain.com      => Will stay as is, secured with no errors.
https://www.domain.com  => This is the only thing that will give a user a notice.
                           Luckily, I don't see any user typing this into the browser.

我已经从我的DNS中移除了以下内容,因为除非我在另一个CERT上投入更多资金,否则我会附带www.subdomain.domain.com不实用的条款,但如果他们使用此域名我会很失败www

CNAME     www.subdomain    subdomain.domain.com.

现在这是我的新VirtualHost *:80我用我问题中的那个替换了。{/ p>

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</VirtualHost>

再次感谢Jon Lin回应并帮助我。