2

I have the following statement and it works perfectly to redirect all requests for "www" on :80 to force the browser to the same location on :443

RewriteEngine   on
RewriteCond     %{HTTPS} !=on
RewriteRule     ^/(.*)$ https://www.mydomain.com/$1 [R=301,L]

However, what I want to do is, regardless of what is between https:// and .mydomain.com - in this case the www, but it could be anything, like "www2", or "dev" etc - I want the URL simply converted to it's SSL equivalent.

I'm new to mod_rewrite so is what I'm looking to do even possible given that the subdomain could be any variable length of characters?

Thanks in advance for any help.

rws907
  • 231
  • 2
  • 8
  • See [this](http://stackoverflow.com/questions/3659732/modrewrite-with-https) post for details on this issue. – krg Oct 07 '12 at 20:01

4 Answers4

1

Try:

RewriteEngine   on
RewriteCond     %{HTTPS} !=on
RewriteRule     ^/*(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
geocar
  • 2,317
  • 14
  • 10
  • That change keeps the redirect but when I create a new subdomain, in this case, http://test.mydomain.com the redirect switches to 443 like I want but instead of showing index of test.mydomain.com it redirects to https://www.mydomain.com. However, if I remove the requirement rewrite rules for test.mydomain.com:80 I can view http://test.mydomain.com without any problem. It seems that once the 443 force is used, it pushes all requests to the document root of www.mydomain.com –  Oct 07 '12 at 20:35
  • @RyanS - `HTTP_HOST` doesn't know anything about "subdomains", it contains the entire name supplied in the URL. That means http://test.example.com will redirect to https://test.example.com/ – geocar Oct 07 '12 at 20:59
  • I understand, what I meant to say was that I tried using your solution and when I applied it and restarted Apache, the behavior I mentioned happened but I don't see how it could given that, as you said, HTTP_HOST uses the entire entered URL yet when I tried going to http://test.mydomain.com the rewrite rule sent me to https://www.mydomain.com. Makes no sense. –  Oct 07 '12 at 21:24
  • @RyanS - no it didn't. Enable [RewriteLog](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging) to convince yourself. – geocar Oct 08 '12 at 20:48
  • You were right, I turned on logging and now see this error message: [Mon Oct 08 14:32:51 2012] [warn] _default_ VirtualHost overlap on port 443, the first has precedence –  Oct 08 '12 at 21:21
1

By using environment variables provided by Apache mod_rewrite:

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

See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond for more information.

  • Anyone else have any insights? SO far nothing has worked. –  Oct 08 '12 at 16:24
  • Why? This is actual code from virtual host declaration. Do you have errors or it simply does not work? –  Oct 08 '12 at 18:41
  • It simply doesn't work; I'm getting no errors If you look below at Peter's answer you'll see I replied with the actual code I used in my host config files for both www:80 and test:80. Thanks for all the help so far. –  Oct 08 '12 at 19:04
0

You could use the HTTP_HOST variable in your substitution string:

RewriteEngine   on

RewriteCond     %{HTTPS} !=on
RewriteCond     %{HTTP_HOST} mydomain\.com$
RewriteRule     ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Peter
  • 101
  • 2
  • So far I have had no luck with any of the alternatives. The directives for my www.mydomain.com on :80 are as follows RewriteEngine on RewriteCond %{HTTPS} !=on RewriteRule ^/*(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] I have the exact same entries at the top of the directives for the test.mydomain.com on :80 but with them enabled on the "test" sub-domain everything redirects to https://www.mydomain.com/$1. If I remove the rewrite from :80 on "test" then I can view it no problem. –  Oct 08 '12 at 01:43
0

After turning on the logging at geocar's advice and doing some research I found the issue to be with a missing statement in /etc/apache2/ports.conf that was causing the overlap on :443. I resolved that and the problem is gone. Thanks to everyone for the insight and help.

rws907
  • 231
  • 2
  • 8