F
F
fdroid2016-07-23 21:04:35
Debian
fdroid, 2016-07-23 21:04:35

How to redirect from http to https in apache?

Hello. There is a NAS running Debian 8. According to the manuals found on the internet, I installed Zoneminder and Owncloud. Everything works, there is access from the external network via IP to these services, of the form xx.xx.xx.xx/zm and xx.xx.xx.xx/owncloud . To make it look nice, I bought a domain, registered with the registrar a binding to my IP, set up SSL using self-signed certificates. There was access via https, to addresses like https://mydomain.ru/zm , etc. The problem is that I can’t set up automatic redirection so that when I type in the address bar, just mydomain.ru/zm or http:/ /mydo main.ru/zmredirected to https. I smoked a lot of instructions from the Internet, and still didn’t understand how to set it up, where and what to write down - the instructions differ from each other, it seems that sometimes it’s completely drastically. Either the described paths to the files are missing or do not match, or the solutions simply do not work.

<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 [email protected]
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 zdesmoydomen.ru
RedirectMatch permanent ^/(.*) https://zdesmoydomen.ru/$1

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

</VirtualHost>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Mokrinsky, 2016-07-23
@fdroid

<VirtualHost *:80>
  ServerName mydomain.ru
  RedirectMatch permanent ^/(.*) https://mydomain.ru/$1
</VirtualHost>

And for the HTTPS virtual host, add HSTS so that the browser does not even think about HTTP.
PS For HTTPS, you can get certificates for free, for example, LetsEncrypt or StartSSL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question