2

I want to forward all traffic to https. There can be subdomains and subpaths in the URL that should stay intact. Example:

http://subdomain.myDomain.me -> https://subdomain.myDomain.me
http://myDomain.me/subpath -> https://myDomain.me/subpath
http://subdomain.myDomain.me/subpath -> https://subdomain.myDomain.me/subpath

I tried the examples in this neat evaluator (link from here) with the following code:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https:/$1

In the evaluator, everything is fine. The real virtual host looks like this:

<virtualHost *:80>
    ServerName myDomain.me
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https:/$1
</VirtualHost>

When trying to acces the real site, this happens:

http://subdomain.myDomain.me -> http://subdomain.myDomain.me # fail - no https
http://myDomain.me/subpath -> https://myDomain.mesubpath # fail - subpath appended to top-level domain
http://subdomain.myDomain.me/subpath -> https://subdomain.myDomain.me/subpath # success

What is wrong with this rewrite?

Pascal
  • 123
  • 3

2 Answers2

1

The correct rule is the following.

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

This however is not the recommended method to redirect HTTP requests to HTTPS. The preferred method is to use a redirect in your Apache confit to point to the SSL enabled site.

You can more about the preferred method at the Apache Httpd wiki.

user5870571
  • 2,900
  • 2
  • 11
  • 33
  • Thank you. Your code works when I use Chrome. Interestingly, with Firefox (my default) it's not working. The redirect approach seems not suitable for me as I don't want to add rules for every subdomain and subpage - I just want them to be forwarded to their https counterpart. – Pascal Mar 05 '16 at 23:01
1
RewriteEngine On
#RewriteCond %{HTTPS} off # you can skip this if you want to redirect everything
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

If that's not working try:

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

Don't forget to send the R=301 flag to make the redirect permanent.

Ludwig Behm
  • 161
  • 7