2

I am trying to redirect all www and non-https requests to the HTTPS only versions with Apache server. I don't need or want www or non SSL URLs at all. The issue is that with the 2 different snippets below I only get correct http to https redirect but I can still get www to be accessible on sub-domains. The primary domain may only work as it is a Wordpress site and it does it's own redirects. What am I doing wrong here? Should I have ServerAlias with www version of the domain and sub-domains or just non-www ServerName?

#       RewriteCond %{SERVER_NAME} =example.com [OR]
#       RewriteCond %{SERVER_NAME} =*.example.com [OR]
#       RewriteCond %{SERVER_NAME} =www.example.com [OR]
#       RewriteCond %{SERVER_NAME} =www.*.example.com
#       RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

        RewriteEngine On
        RewriteCond %{HTTPS} off [OR]
        RewriteCond %{HTTP_HOST} ^www\. [NC]
        RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
        RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

1 Answer 1

2

I would implement it as two rule sets. First I would redirect any www URL then in a second rule I would redirect HTTP.

RewriteEngine On

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

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [END,NE,R=301]

No request should ever hit both rules. If it has a www it would be stripped and redirected to HTTPS. Otherwise if it has HTTP it will be redirected to HTTPS.

These rules need to be included in each and every virtual hosts file. If you have general rewrite rules outside a virtual host, they will work for all hosts unless that host has other rewrite rules. By default rewrite rules in a virtual host replace the global rules rather than add on to them.

If you did want global rewrite rules to work, you could put the rules outside a virtual host so that they apply globally with the InheritDown rewrite option in each virtual host that had its own other rules. (See How can I implement a global RewriteCond / RewriteRule in Apache that applies to all virtual hosts?.)

6
  • I just tried but if I try for example "https:// www.sub.example.com/" it still does not strip the www part. I am guessing it's because ^ will prevent matches with https in front?
    – DominicM
    Jan 6, 2019 at 12:27
  • 1
    No, it is only matching against the host, not including the http:// My guess is that you are putting the rules in a place that doesn't apply to both the domain and subdomains. For example you may be placing these rules in your main site .htaccess while your subdomain has a different virtual host with a different document root. You may need to add rules to its .htaccess as well. Jan 6, 2019 at 12:36
  • I am adding it to 000-default.conf and yes it seems like these rules do not affect anything at all. I have no other rewrite rules in any config files I can see. The only .htaccess is in /var/www/html (Wordpress site). All sub-domains are in /var/www/sub directories and have no htaccess files. I would like a catch all default rewrite, why isn't the default file being applied in this case?
    – DominicM
    Jan 6, 2019 at 13:55
  • 1
    The default virtual host contains rules for when no other virtual host file matches. You could put the rules outside a virtual host so that they apply globally with the InheritDown rewrite option. (See How can I implement a global RewriteCond / RewriteRule in Apache that applies to all virtual hosts?.) Jan 6, 2019 at 17:27
  • 1
    For example in /etc/apache2/apache2.conf (or the equivalent on your distribution, the file name is different on different flavors of Linux.) On Ubuntu/Debian I would actually create a new configuration file in /etc/apache2/conf-available/ and use sudo a2enconf <name> to enable it. Jan 6, 2019 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.