无法在Apache上将https non-www重定向到www

时间:2019-09-20 11:19:51

标签: apache .htaccess ssl https

因此,我运营着几个域,几个月前,我开始将所有域都切换到SSL。

至少在理论上,在我的DNS控制台(Namecheap)上,我将所有流量都重定向到https&www。

在我的Apache配置中,我有一系列端口80虚拟主机,这些主机将http重定向到https和www。

我的ssl虚拟主机都服务https://www.example.comhttps://example.com

我所有的东西都在www子域中。

我注意到https&non-www只是在所有域上都超时。

所以我在htaccess中尝试了一个重写规则,例如:

RewriteEngine On

# v second thing i tried
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

# v 1st thing i tried
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]

一个单独的虚拟主机,可以重定向流量,如下所示:

        <VirtualHost *:443>
                ServerName example.com

                Redirect / https://www.example.com/

                # Please imagine the Let's encrypt blob here.
        </VirtualHost>


全部无济于事。

我有点迷茫,我在互联网上看到解决方案,它们基本上不起作用,或者我可能没有正确实施它们。谁知道。 (这是我个人不喜欢的一种比喻,但如果你们愿意的话,那就太好了!)

再次说明一下,http非www以及将http www重定向到https&www都可以正常工作。只是https&non-www失败,对我来说超时。

你能帮帮我吗? 预先谢谢你。

编辑:现在粘贴所有配置,以便您获得更好的画面。 .htaccess进行了稍微修改,因为我已经在该特定网站上进行了测试,但是它与我拥有的其他每个.htaccess大致相同,因为我真的很喜欢此配置,所以就这样:

Options -Indexes
Options +FollowSymLinks


<IfModule mod_rewrite.c>
RewriteEngine On
LogLevel alert rewrite:trace8

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L] 

</IfModule>


# Client Cache-control policy:
# Public Cache everything for 1 week (public: can be cached by proxies.)
<IfModule mod_headers.c>
  <FilesMatch "\.(ico|gif|jpg|jpeg|png|pdf|css|js)$">
      Header set Cache-Control "max-age=604800, public"
  </FilesMatch>
</IfModule>

# Expires caching.
# Replicates the same policy seen in the Cache-control section.
<IfModule mod_expires.c>
  <FilesMatch "\.(ico|jpe?g|png|gif|js|css|pdf)$">
    ExpiresActive On
    ExpiresByType image/jpg "access 1 week"
    ExpiresByType image/jpeg "access 1 week"
    ExpiresByType image/gif "access 1 week"
    ExpiresByType image/png "access 1 week"
    ExpiresByType text/css "access 1 week"
    ExpiresByType application/pdf "access 1 week"
    ExpiresByType application/javascript "access 1 week"
    ExpiresByType text/x-javascript "access 1 week"
    ExpiresByType image/x-icon "access 1 week"
  </FilesMatch>
</IfModule>

再次,这是我在/ var / www / apache2 / sites-enabled中拥有的我的example.conf小文件,与其他文件大致相似:

<IfModule mod_ssl.c>
    # This works amazing.
    <VirtualHost *:80>
        ServerName www.example.com
        ServerAlias example.com

        Redirect / https://www.example.com/
    </VirtualHost>


    <VirtualHost *:443>
        ServerName www.example.com # Works
        ServerAlias example.com # Doesn't work no matter what.


        DocumentRoot "/var/www/example"

        Options +FollowSymLinks 

            <Directory />
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
        Allow from all
        </Directory>

        <Directory "/var/www/example">
        Options IncludesNOEXEC
        AllowOverride All
        Order allow,deny
        Allow from all
        </Directory> 



        #LogLevel info ssl:warn # trace8.... trace1, debug, info, notice, warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # The lets encrypt blob.
        SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem

        SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem

        Include /etc/letsencrypt/options-ssl-apache.conf

    </VirtualHost>
</IfModule>

编辑:抱歉,我意识到无法在.htaccess中配置LogLevel,并将其移动到我的虚拟主机中,否则我的配置现在是相同的,我正在对其进行测试。

编辑:这是我执行此操作并请求https&non-www导致超时后的日志:

[Fri Sep 20 10:20:32.829860 2019] [rewrite:trace3] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] strip per-dir prefix: /var/www/example/ -> 
[Fri Sep 20 10:20:32.829900 2019] [rewrite:trace3] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] applying pattern '(.*)' to uri ''
[Fri Sep 20 10:20:32.829921 2019] [rewrite:trace4] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] RewriteCond: input='www.example.com' pattern='^example.com$' [NC] => not-matched
[Fri Sep 20 10:20:32.829927 2019] [rewrite:trace1] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] pass through /var/www/example/
[Fri Sep 20 10:20:32.830005 2019] [rewrite:trace1] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71dd0a0/subreq] [perdir /var/www/example/] pass through /var/www/example/index.html
[Fri Sep 20 10:20:32.830044 2019] [rewrite:trace1] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71dd0a0/subreq] [perdir /var/www/example/] pass through /var/www/example/index.cgi
[Fri Sep 20 10:20:32.830072 2019] [rewrite:trace1] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71dd0a0/subreq] [perdir /var/www/example/] pass through /var/www/example/index.pl
[Fri Sep 20 10:20:32.830100 2019] [rewrite:trace1] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71dd0a0/subreq] [perdir /var/www/example/] pass through /var/www/example/index.php
[Fri Sep 20 10:20:33.867992 2019] [rewrite:trace3] [pid 21314] mod_rewrite.c(475): [client 188.26.215.200:51252] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] strip per-dir prefix: /var/www/example/wp-includes/css/admin-bar.min.css -> wp-includes/css/admin-bar.min.css, referer: https://www.example.com/
[Fri Sep 20 10:20:33.868055 2019] [rewrite:trace3] [pid 21314] mod_rewrite.c(475): [client 188.26.215.200:51252] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] applying pattern '(.*)' to uri 'wp-includes/css/admin-bar.min.css', referer: https://www.example.com/
[Fri Sep 20 10:20:33.868074 2019] [rewrite:trace4] [pid 21314] mod_rewrite.c(475): [client 188.26.215.200:51252] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] RewriteCond: input='www.example.com' pattern='^example.com$' [NC] => not-matched, referer: https://www.example.com/
[Fri Sep 20 10:20:33.868080 2019] [rewrite:trace1] [pid 21314] mod_rewrite.c(475): [client 188.26.215.200:51252] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] pass through /var/www/example/wp-includes/css/admin-bar.min.css, referer: https://www.example.com/
[Fri Sep 20 10:20:33.936995 2019] [rewrite:trace3] [pid 21314] mod_rewrite.c(475): [client 188.26.215.200:51252] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] strip per-dir prefix: /var/www/example/wp-content/themes/portfolio-web/assets/library/bootstrap/css/bootstrap.min.css -> wp-content/themes/portfolio-web/assets/library/bootstrap/css/bootstrap.min.css, referer: https://www.example.com/
[Fri Sep 20 10:20:33.937035 2019] [rewrite:trace3] [pid 21314] mod_rewrite.c(475): [client 188.26.215.200:51252] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] applying pattern '(.*)' to uri 'wp-content/themes/portfolio-web/assets/library/bootstrap/css/bootstrap.min.css', referer: https://www.example.com/
[Fri Sep 20 10:20:33.937055 2019] [rewrite:trace4] [pid 21314] mod_rewrite.c(475): [client 188.26.215.200:51252] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] RewriteCond: input='www.example.com' pattern='^example.com$' [NC] => not-matched, referer: https://www.example.com/
[Fri Sep 20 10:20:33.937067 2019] [rewrite:trace1] [pid 21314] mod_rewrite.c(475): [client 188.26.215.200:51252] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] pass through /var/www/example/wp-content/themes/portfolio-web/assets/library/bootstrap/css/bootstrap.min.css, referer: https://www.example.com/
[Fri Sep 20 10:20:33.939182 2019] [rewrite:trace3] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] strip per-dir prefix: /var/www/example/wp-content/themes/portfolio-web/assets/library/Font-Awesome/css/font-awesome.min.css -> wp-content/themes/portfolio-web/assets/library/Font-Awesome/css/font-awesome.min.css, referer: https://www.example.com/
[Fri Sep 20 10:20:33.939212 2019] [rewrite:trace3] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] applying pattern '(.*)' to uri 'wp-content/themes/portfolio-web/assets/library/Font-Awesome/css/font-awesome.min.css', referer: https://www.example.com/
[Fri Sep 20 10:20:33.939231 2019] [rewrite:trace4] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] RewriteCond: input='www.example.com' pattern='^example.com$' [NC] => not-matched, referer: https://www.example.com/
[Fri Sep 20 10:20:33.939260 2019] [rewrite:trace1] [pid 21313] mod_rewrite.c(475): [client 188.26.215.200:51250] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] pass through /var/www/example/wp-content/themes/portfolio-web/assets/library/Font-Awesome/css/font-awesome.min.css, referer: https://www.example.com/
[Fri Sep 20 10:20:34.036167 2019] [rewrite:trace3] [pid 21312] mod_rewrite.c(475): [client 188.26.215.200:51258] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] strip per-dir prefix: /var/www/example/wp-includes/js/admin-bar.min.js -> wp-includes/js/admin-bar.min.js, referer: https://www.example.com/
[Fri Sep 20 10:20:34.036236 2019] [rewrite:trace3] [pid 21312] mod_rewrite.c(475): [client 188.26.215.200:51258] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] applying pattern '(.*)' to uri 'wp-includes/js/admin-bar.min.js', referer: https://www.example.com/
[Fri Sep 20 10:20:34.036267 2019] [rewrite:trace4] [pid 21312] mod_rewrite.c(475): [client 188.26.215.200:51258] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] RewriteCond: input='www.example.com' pattern='^example.com$' [NC] => not-matched, referer: https://www.example.com/
[Fri Sep 20 10:20:34.036279 2019] [rewrite:trace1] [pid 21312] mod_rewrite.c(475): [client 188.26.215.200:51258] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] pass through /var/www/example/wp-includes/js/admin-bar.min.js, referer: https://www.example.com/
[Fri Sep 20 10:20:34.119597 2019] [rewrite:trace3] [pid 21312] mod_rewrite.c(475): [client 188.26.215.200:51258] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] strip per-dir prefix: /var/www/example/wp-content/uploads/2019/09/portfolioyo2.jpg -> wp-content/uploads/2019/09/portfolioyo2.jpg, referer: https://www.example.com/
[Fri Sep 20 10:20:34.119667 2019] [rewrite:trace3] [pid 21312] mod_rewrite.c(475): [client 188.26.215.200:51258] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] applying pattern '(.*)' to uri 'wp-content/uploads/2019/09/portfolioyo2.jpg', referer: https://www.example.com/
[Fri Sep 20 10:20:34.119682 2019] [rewrite:trace4] [pid 21312] mod_rewrite.c(475): [client 188.26.215.200:51258] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] RewriteCond: input='www.example.com' pattern='^example.com$' [NC] => not-matched, referer: https://www.example.com/
[Fri Sep 20 10:20:34.119687 2019] [rewrite:trace1] [pid 21312] mod_rewrite.c(475): [client 188.26.215.200:51258] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e30a0/initial] [perdir /var/www/example/] pass through /var/www/example/wp-content/uploads/2019/09/portfolioyo2.jpg, referer: https://www.example.com/
[Fri Sep 20 10:20:34.122833 2019] [rewrite:trace3] [pid 21311] mod_rewrite.c(475): [client 188.26.215.200:51288] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] strip per-dir prefix: /var/www/example/wp-content/uploads/2019/01/laptop-1209008.jpg -> wp-content/uploads/2019/01/laptop-1209008.jpg, referer: https://www.example.com/
[Fri Sep 20 10:20:34.122907 2019] [rewrite:trace3] [pid 21311] mod_rewrite.c(475): [client 188.26.215.200:51288] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] applying pattern '(.*)' to uri 'wp-content/uploads/2019/01/laptop-1209008.jpg', referer: https://www.example.com/
[Fri Sep 20 10:20:34.122933 2019] [rewrite:trace4] [pid 21311] mod_rewrite.c(475): [client 188.26.215.200:51288] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] RewriteCond: input='www.example.com' pattern='^example.com$' [NC] => not-matched, referer: https://www.example.com/
[Fri Sep 20 10:20:34.122941 2019] [rewrite:trace1] [pid 21311] mod_rewrite.c(475): [client 188.26.215.200:51288] 188.26.215.200 - - [www.example.com/sid#7f44b72a56b8][rid#7f44b71e70a0/initial] [perdir /var/www/example/] pass through /var/www/example/wp-content/uploads/2019/01/laptop-1209008.jpg, referer: https://www.example.com/

0 个答案:

没有答案