Перенаправить www в https после того, как испортил конфигурационные файлы - PullRequest
0 голосов
/ 07 мая 2018

Я получил сертификат SSL от Let's Encrypt, и после того, как я запутал установку ошибочными настройками в процессе установки, я считаю, что в файле 000-default.conf есть неработающий код, потому что в настоящее время, только если я набираю example.com, он перенаправляет меня на https://example.com/, но когда я набираю www.example.com, это оставляет меня на www.example.com без HTTPS. вот так 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.example.com

    ServerAdmin webmaster@localhost
    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
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

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

Edit: Let's Encrypt создал больше файлов внутри sites-available, кроме 000-default.conf, поэтому я предполагаю, что вам нужны все они для совместной работы, например, следующий файл: 000-default-le-ssl.conf:

<IfModule mod_ssl.c>
<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 www.example.com

    ServerAdmin webmaster@localhost
    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
ServerName example.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

У меня есть сертификат для www.example.com и example.com. Что мне нужно отредактировать, чтобы запретить www.example.com доступ к обычному HTTP и перенаправить его на HTTPS? Я не могу узнать

Спасибо

Ответы [ 2 ]

0 голосов

Вы можете просто сказать ему перенаправить порт 80 на порт 443, вот где я предполагаю, что вы используете свой ssl, вот пример:

https://wiki.apache.org/httpd/RedirectSSL

0 голосов
/ 07 мая 2018

Вы хотите включить имя сервера и псевдоним в ваш виртуальный хост

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  ServerName website.com
  ServerAlias www.website.com

  RewriteEngine On 
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]    
</VirtualHost>

Это заставит перенаправить оба URL. Обратите внимание, что раньше ваше правило ограничивалось URL-адресами без www.

...