我最近购买了一个 VPS 和一个域名。VPS 运行的是 Ubuntu 16.04 和 Apache 2.4。当我在浏览器中输入 时http://example.com
,索引页可以正确加载。如果我www
在域名前输入 ,也会发生同样的事情,但在这种情况下www
会消失。
如何让它www
停留在地址栏中并重定向所有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, e.g.
#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 仍然没有显示。