Can't get auto redirect to https to work

Hey guys!
I just installed a let’s encrypt certificate on my server and am really satisfied and impressed!
Although I’m pretty sure that I said yes to the prompt if i want to redirect all traffic to https, it’s not activated.
Unfortunately i’m not able to get it work by myself.
I just don’t know where to put the redirection code into.
Right now I’m using .htacces to redirect but I know that it would be better to do it by the apache conf file.
Putting this snippet into vhosts.conf gives me several errors, depending on the browser:

<Virtualhost .domain.com>

ServerName domain.com
DocumentRoot /var/www/

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


If I paste it to le-domain.com in sites-available, nothing happens.
Not even an error.

I’d really appreciate some help!

Best regards,
Dominik

PS: I have currently only one domain.

1 Like

Hi @themrdomi,

It should be easy to help you if you paste the unedited conf of your virtual host, anyway, the redirect should be performed inside the virtualhost that manages yout domain on port 80. You only need to add

Redirect permanent / https://domain.com/

As I said, it is a conf really strange. Inside your VirtualHost directive you have .domain.com, that dot in front of your domain is really strange.

Hey,
thanks for your answer!
Here are the requested files:
I put the redirect you posted into the /etc/apache2/sites-available/000-default.conf but nothing happened, no error, nothing. I just don’t know where to put it in because there are several files containing that domain port 80 code.
That dot that you found was a writing mistake from me, it’s not in the server configuration.

/etc/apache2/apache2.conf
http://carboncore.de/documents/apache2.conf

/etc/apache2/sites-enabled/000-default.conf
http://carboncore.de/documents/sites-enabled/000-default.conf

/etc/apache2/sites-enabled/default-ssl.conf
http://carboncore.de/documents/sites-enabled/default-ssl.conf

/etc/apache2/sites-available/000-default.conf
http://carboncore.de/documents/sites-available/000-default.conf

/etc/apache2/sites-available/default-ssl.conf
http://carboncore.de/documents/sites-available/default-ssl.conf

/etc/apache2/sites-available/le-redirect-domain.com.conf
http://carboncore.de/documents/sites-available/le-redirect-domain.com.conf

/etc/apache2/vhosts.conf
–> Is completely empty.

Regards,
Dominik

Hello @themrdomi,

Why do you use different files for sites-available and sites-enabled?. It is supposed that “files” inside sites-enabled are symbolic links to sites-available, something like this:

ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 35 May 22 19:43 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 35 May 22 19:52 default-ssl.conf -> ../sites-available/default-ssl.conf

So you create and modify your files inside sites-available and when you want to activate it you simple use the command a2ensite to enable it or a2dissite to disable it.

Examples:

# ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 35 May 22 19:43 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 35 May 22 19:52 default-ssl.conf -> ../sites-available/default-ssl.conf

# a2dissite 000-default
Site 000-default disabled.
To activate the new configuration, you need to run:
  service apache2 reload

# ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 35 May 22 19:52 default-ssl.conf -> ../sites-available/default-ssl.conf

# a2ensite 000-default
Enabling site 000-default.
To activate the new configuration, you need to run:
  service apache2 reload

# ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 35 May 22 20:07 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 35 May 22 19:52 default-ssl.conf -> ../sites-available/default-ssl.conf

Anyway, you have created the redirect in sites-available/000-default.conf but the file sites-enabled/000-default.conf is not pointing to sites-available/000-default.conf, it is an independent file that doesn’t have the Redirect directive. The files inside sites-available are not loaded, only the ones in sites-enabled.

Cheers,
sahsanu

Hi,
thanks so much.
Can you tell me how I can relate the files to each other or what I have to add to the files?
It’s a little awkward since I haven’t deleted anything from those files?

Cheers,
Dominik

@themrdomi, sorry but I don’t understand what you want to do.

Hey,
you told me, that the file sites-enabled/000-default.conf is not pointing to sites-available/000-default.conf although it should.
So how can I do that, how can i let the file point to the other file or how can the sites-available files be loaded?

Cheers,

Never mind, i figured it out: ln -s target linkname
Unfortunately I’m getting an error now: ERR_TOO_MANY_REDIRECTS
Any idea how that is being caused?

Cheers,

Yes, because you provided these files:
http://carboncore.de/documents/sites-enabled/000-default.conf that didn't contain the Redirect directive and http://carboncore.de/documents/sites-available/000-default.conf that contain the Redirect directive. So, for me the files were different.

Maybe I didn't explain it very well, but those links are created using the command line tools a2ensite and a2dissite, with these tools you are able to create and remove the symbolic links in sites-enabled.

Did you remove the .htaccess rewrite rules that you were using?.

Cheers,
sahsanu

Edit: I got it to work now. I opened the le-redirect-domain.com.conf file using nano and then closed it again without changing anything. After reloading apache, it all worked somehow. After pasting Redirect permanent / https://domain.com/ into 000-deafult.conf it is working fine now. Was propably a bug or I was just to dumb to do it. Now it is all working. Thanks for the help.
One question: What do I need the le-redirect-domain.com.conf file for? Since I have the redirect code in 000-default.conf…

Hi,
thanks for your answer.
That was propably a misunderstanding. I enabled the sites now using the a2ensite tool.
Concerning the err_too_many_redirects: It was indeed because of the .htaccess file I was using.
Unfortunately, after removing the redirection in the .htaccess, the main site of domain.com is now /var/www/html instead of just /var/www.
I have searched every file in /etc/apache2/sites-available/ but the root folder is always set as /var/www/.
No idea why I’m being redirected to that site…
https://domain.com is working prefectly fine. So it’s only for port 80. But I can ensure that there is no line such as ‘/var/www/html’ inside sites-available.
Cheers,

As far as I know you don't need it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.