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.

1
  • See this post for details on this issue.
    – krg
    Oct 7, 2012 at 20:01

4 Answers 4

1

Try:

RewriteEngine   on
RewriteCond     %{HTTPS} !=on
RewriteRule     ^/*(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
5
  • That change keeps the redirect but when I create a new subdomain, in this case, test.mydomain.com the redirect switches to 443 like I want but instead of showing index of test.mydomain.com it redirects to mydomain.com. However, if I remove the requirement rewrite rules for test.mydomain.com:80 I can view 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
    – rsmith84
    Oct 7, 2012 at 20:35
  • @RyanS - HTTP_HOST doesn't know anything about "subdomains", it contains the entire name supplied in the URL. That means test.example.com will redirect to test.example.com
    – geocar
    Oct 7, 2012 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 test.mydomain.com the rewrite rule sent me to mydomain.com. Makes no sense.
    – rsmith84
    Oct 7, 2012 at 21:24
  • @RyanS - no it didn't. Enable RewriteLog to convince yourself.
    – geocar
    Oct 8, 2012 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
    – rsmith84
    Oct 8, 2012 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.

3
  • Anyone else have any insights? SO far nothing has worked.
    – rsmith84
    Oct 8, 2012 at 16:24
  • Why? This is actual code from virtual host declaration. Do you have errors or it simply does not work?
    – huksley
    Oct 8, 2012 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.
    – rsmith84
    Oct 8, 2012 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]
1
  • 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 mydomain.com/$1. If I remove the rewrite from :80 on "test" then I can view it no problem.
    – rsmith84
    Oct 8, 2012 at 1: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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .