Redirect www.example.com to example.com

 https://stackoverflow.com/questions/39303725/virtual-host-force-https-and-redirect-www-to-non-www-but-no-other-subdomains

With the proper use of regular expressions, the redirection of www to a non-www domain is quite simple:

Listen for incoming HTTP connections

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com

    RewriteEngine on

    # Redirect both www.example.com and example.com to https://example.com
    RewriteCond %{HTTP_HOST} ^(www\.)?(.*) [NC]
    RewriteRule ^ https://%2%{REQUEST_URI} [L,R=301]
</VirtualHost>

Listen for incoming HTTPS connections

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com

    RewriteEngine on

    # Redirect www.example.com to example.com
    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</VirtualHost>

These virtual host directives only apply to URLs starting with domain.com and www.domain.com because incoming requests must match the ServerName or ServerAlias. This means that any subdomain such as sub.example.com cannot be a successful match.

Không có nhận xét nào:

Check to be sure that port 22 is enabled before you reload

 https://superuser.com/questions/590600/ufw-is-active-but-not-enabled-why etting  ENABLED=yes  in  /etc/ufw/ufw.conf  did it for me. $ sudo ...