在地址栏中显示www的问题

我最近购买了一个VPS和一个域名。 VPS使用Apache 2.4运行Ubuntu 16.04。 当我进入浏览器并inputhttp://example.com ,索引页面正确加载。 同样的事情发生,如果我把www之前的域名,但在这种情况下www消失。

如何使地址栏中的www粘住并可能redirect所有non-www请求? 这对我来说至关重要,因为我已经为包含www的域名购买了SSL证书。

我试图修改DNS设置,以达到目的,但到目前为止,我所有的尝试都失败了。

我目前的(工作)设置是:

 === A records === [empty] 31.14.131.29 localhost 127.0.0.1 webmail 62.149.158.92 webmail 62.149.158.91 www 31.14.131.29 === AAAA records === None === CNAME records === None 

另外我也尝试过使用.htaccess ,但显然这些请求都是在一个循环中被捕获的。

这是我第一次设置VPS,感觉有点失落。 你能告诉我正确的做法吗?

编辑000-default.conf文件内容

 <VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName www.grandegiove.com ServerAdmin [email protected] DocumentRoot /var/www/html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, eg #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost> 

编辑.htaccess内容文件

 Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^grandegiove.com[nc] RewriteRule ^(.*)$ http://www.grandegiove.com/$1 [l,r=301,nc] 

仍然www不显示。