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

时间:2017-01-19 16:24:07

标签: apache .htaccess https virtualhost

我使用Apache Virtual Server在VPS上托管了一个网站,因为我计划稍后在该VPS上托管更多网站。虚拟主机正常工作,直到我使用LetsEncrypt Certbot在其上安装SSL。完成后,https://techkernel.org工作正常,但http变体http://techkernel.org被重定向到默认的Apache网页。虚拟主机配置无法正常工作,并且会定向到默认的/ var / www / html目录。

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

相关信息:

等/ apache2的/位点可用/ techkernel.org.conf

onResume()

无功/网络/ techkernel.org /公共/ htaccess的

<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

ls -l / etc / apache2 / sites-enabled

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [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

3 个答案:

答案 0 :(得分:1)

您已将配置粘贴为来自网站的示例 - 可用。您是否已将其符号链接到网站 - 实际上已启用,启用它?

编辑 - 对不起,这似乎不必要地嗤之以鼻。我的观点是您的HTTPS变体正在运行,并且位于单独的配置文件中。您的HTTP不是,在我看来是因为您还没有创建此符号链接,因此Apache没有配置&#34;当您在端口80上接收请求并将Host:标头设置为techkernel时执行的操作。 org&#34;,因此它会进入默认站点。你的配置对我来说看起来非常好,它实际上并没有被Apache使用。

答案 1 :(得分:1)

除了您在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}/$1 [NC,R=301,L]
如果设置了UseCanonicalName On,则

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

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

如果修复了,您可以/应该删除.htaccess

中有关此内容的所有条目
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]

答案 2 :(得分:0)

原来,000-default.conf搞得一团糟。只有在运行apachectl -S命令后才会注意到它。修好后(输入正确的ServerNameDocumentRoot以及一些网址重写条件)和reload apache2,它完美无缺。