简体   繁体   中英

redirect www to non-www https with apache & .htaccess

I've seen a lot of these questions being asked but I have yet to find one that works for my scenario. I have tried almost every single one of them and just can't get it to work.

Could someone explain how I can get www.domain.com and www.subdomain.domain.com to redirect to https without the www?

Here is what I have so far:

Currently, I have the following DNS records:

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

I also have a virtual host file as follows below (almost exact replica's for subdomain as well) :

<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>

In the 443 virtualhost, try adding:

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

So this is what i've learned. www.domain.com is different than domain.com . This is includes subdomains too. Ideally, you are going to want to have an SSL certification for both of these names. If you are like me, however, I didn't really want to spend the money.

The following fix works if a user types the following into the address bar:

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.

I've removed the following from my DNS because I've come with terms that www.subdomain.domain.com isn't practical unless I spend more money on another CERT, but I'm fine with this domain failing if they use a www .

CNAME     www.subdomain    subdomain.domain.com.

And this is now my new VirtualHost *:80 that I replaced with the one in my question.

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

Thanks again, to Jon Lin for responding and helping me out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM