我正在尝试重定向到使用 URL www.example.com 的网站,但它给了我 403 禁止访问。该网站适用于 example.com,https://www.example.com https://example.com
这是我的虚拟主机:
<VirtualHost :*80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /home/asd/sites/example.com/public_html
<Directory /home/asd/sites/example.com/public_html>
AllowOverride all
Options Indexes FollowSymLinks Includes ExecCGI
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:443>
# 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 example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /home/asd/sites/example.com/public_html
SSLEngine On
SSLCertificateFile /home/asd/ssl_certs/example.com.crt
SSLCertificateKeyFile /home/asd/ssl_certs/example.com.key
SSLCACertificateFile /home/asd/ssl_certs/example.com.intermediate.crt
<Directory /home/asd/sites/example.com/public_html>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
AllowOverride all
Options Indexes FollowSymLinks Includes ExecCGI
Require all granted
</Directory>
我也尝试过启用和禁用 000-default.conf。但如果我启用它,那么 www.example 将转到 apache 根目录 index.html
https://www.example.com但工作正常。
我已经尝试让它工作了几个小时,但我无法做到。任何帮助都将不胜感激。
我希望所有流量都从非 http 流向 https、从 www 流向非 www。
谢谢!
答案1
后来发现是打字错误。我花了一整天时间才弄清楚这个问题。
它是:
<VirtualHost :*80>
应该是:
<VirtualHost *:80>