3

I set up a new virtual host for a new site on my server, but it's acting strangely despite having the exact same settings as my original site.

I want all requests to be redirected to HTTPS, simply put. On my main site (the working one) I have this specified in the VirtualHost config, and in .htaccess I have an additional rule specified to allow short URLs. Specifically, the problem on the non-working site is that if I try to go to example.com/url, the redirect goes to https://example.comurl and removes the necessary / from the URL.

I copied the exact config over to the new VirtualHost and .htaccess file from the working site so I'm not sure why it doesn't work on the new one. My DNS records for both sites all use A records to point to it, no redirects or anything happen at the DNS level. I have tried putting a / at the end of the Redirect lines in the code below, but the problem was not resolved. The server is Ubuntu 14.04, and Apache is version 2.4.7. Both sites are separate domain names with separate .conf files used, but they are hosted on the same server with the same IP address. How can I fix this problem?

The relevant code in the VirtualHost:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com
</VirtualHost>

And in .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

In case it helps/matters, this is the SSL config in my VirtualHost file, though it is placed at the very beginning of the file outside of all other directives:

SSLCipherSuite AES128+EECDH:AES128+EDH
SSLProtocol All -SSLv2 -SSLv3
SSLHonorCipherOrder On
Header always set Strict-Transport-Security "max-age=63072000; includeSubdo$
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff

SSLCompression off
SSLUseStapling on
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"

And this is the config specifically for the VirtualHost for the site:

<VirtualHost *:443>
    ServerName example.com
    ServerAdmin user@example.com
    DocumentRoot /var/www/example
    ErrorLog ${APACHE_LOG_DIR}/errorexample.log
    Options -Indexes

    SSLEngine on
    SSLCertificateFile /etc/ssl/example/examplecert.crt
    SSLCertificateKeyFile /etc/ssl/example/examplekey.key
    SSLCertificateChainFile /etc/ssl/chain/class1.pem
</VirtualHost>
3
  • 1
    have you got SSL defined? – td512 May 15 '15 at 19:14
  • 1
    Maybe different Apache versions? And have you tried putting the slash at the end of the Redirect line? Like this? Redirect permanent / https://example.com/ – Marki555 May 15 '15 at 19:19
  • I do have SSL defined, I added the relevant code blocks to the OP. I also did try putting a / at the end of the Redirect command, but that did not fix the problem. – vaindil May 15 '15 at 19:21
4

To redirect to HTTPS:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

But you need to have a virtual host for ssl:

NameVirtualHost *:443
<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    SSLEngine on
    SSLCertificateFile    /etc/apache2/ssl/apache.crt
    SSLCertificateKeyFile /etc/apache2/ssl/apache.key
</VirtualHost>

NOTE: you have to create a SSL certificate... but there is great tutorial for this!

4
  • Huh, this appears to have resolved the problem. I already had the SSL virtual host specified (I suspect you were writing this answer while I was editing mine to add that in), but I changed my Redirect lines to your first code block instead and everything works properly now. Thank you so much! – vaindil May 15 '15 at 19:32
  • happy to help :) – Ricain May 15 '15 at 19:33
  • Can you check this link :askubuntu.com/questions/876767/… – Nullpointer Jan 27 '17 at 6:54
  • where does the rewrite engine stuff go, in the vhosts file? – Andrew Sep 6 '17 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.