Apache 虚拟主机:HTTP 被重定向到默认的 Apache 站点

Apache Virtual Host: HTTP getting redirected to the default Apache site

我已经使用 Apache 虚拟服务器在 VPS 上托管了一个网站,因为我计划稍后在该 VPS 上托管更多网站。在我使用 LetsEncrypt Certbot 在其上安装 SSL 之前,虚拟主机一直正常工作。这样做之后,https://techkernel.org works fine, but the http variant, http://techkernel.org 被重定向到默认的 Apache 网页。虚拟主机配置工作不正常,它被定向到默认的 /var/www/html 目录。

如果您能告诉我如何修复它,使 HTTP 和 HTTPS 请求都重定向到 HTTPS 并显示正确的网站,那将非常有帮助。谢谢。

相关信息:

etc/apache2/sites-available/techkernel.org.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.techkernel.org

    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/techkernel.org/public"

    ServerAlias techkernel.org

    # 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
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.techkernel.org [OR]
    RewriteCond %{SERVER_NAME} =techkernel.org
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

var/www/techkernel.org/public/.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/% [R,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

ls -l /etc/apache2/sites-enabled

lrwxrwxrwx 1 root root 35 Dec 28 11:08 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 38 Jan 21 04:31 techkernel.org.conf -> ../sites-available/techkernel.org.conf
lrwxrwxrwx 1 root root 55 Dec 28 19:46 techkernel.org-le-ssl.conf -> /etc/apache2/sites-available/techkernel.org-le-ssl.conf

ls -l /etc/apache2/sites-enabled

-rw-r--r-- 1 root root 1332 Jul  5  2016 000-default.conf                                                                                                           
-rw-r--r-- 1 root root 6437 Aug  7 10:56 default-ssl.conf                                                                                                           
-rw-r--r-- 1 root root 1574 Jan 19 16:05 techkernel.org.conf                                                                                                        
-rw-r--r-- 1 root root 1613 Dec 28 19:46 techkernel.org-le-ssl.conf

您已将您的配置粘贴为 sites-AVAILABLE 中的示例。您是否已将它符号链接到 sites-ENABLED 以实际启用它?

编辑 - 抱歉,这似乎没有必要。我的观点是您的 HTTPS 变体正在运行,并且位于单独的配置文件中。你的 HTTP 不是,这在我看来是因为你没有创建这个符号链接,因此 Apache 没有 "what you do when you receive a request on port 80 with the Host: header set to techkernel.org" 的配置,因此它正在进入默认站点。您的配置对我来说看起来非常好,只是 Apache 实际上没有使用它。

原来是 000-default.conf 搞砸了。只有在 运行 apachectl -S 命令之后我才注意到它。在修复它(输入正确的 ServerNameDocumentRoot 以及一些 URL 重写条件)和 reloading apache2 之后,它完美地工作。

除了您可能在 000-default.conf 中发现的错误外,您的重写规则还存在以下问题:

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.techkernel.org [OR]
RewriteCond %{SERVER_NAME} =techkernel.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

应该是

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?techkernel.org
RewriteRule ^/(.*) https://%{HTTP_HOST}/ [NC,R=301,L]
如果设置了 UseCanonicalName On,

%{SERVER_NAME} 可能是来自 VirtualHost 配置的值。因此,无论访问者使用什么 URI,它都会重定向所有内容。如果这是意图,那么只需删除任何 RewriteCond 并使用

RewriteEngine on
RewriteRule ^/(.*) https://www.techkernel.org/ [NC,R=301,L]

如果问题得到解决,您 could/should 删除 .htaccess 中的所有相关条目

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/% [R,L]