2

This is my first post to StackOverflow, so please bear with me......

I'm trying to redirect www.example.com to https://example.com. I've looked at quite a few solutions on StackOverflow and other forum sites, but I can't seem to get anything to work. Here are a few of the other StackOverflow pages I've looked at:

Generic htaccess redirect www to non-www

apache redirect from non www to www

Redirect Non-WWW to www

My SSL cert lists both example.com and www.example.com. Here is the code in my apache2.conf file that I've been trying to add the redirect:

<Directory /home/tim/examplepath>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ sort-url.php?rt=$1
</Directory>

<VirtualHost XXX.XXX.XX.XXX:12345>
    ServerName example.com
    Redirect / https://example.com/ 
</VirtualHost>

<VirtualHost XXX.XXX.XX.XXX:80>
   ServerName example.com
   Redirect / https://example.com/
</VirtualHost>

<VirtualHost XXX.XXX.XX.XXX:123>
   ServerName example.com
   DocumentRoot /home/tim/examplepath/
   SSLEngine on
   SSLCertificateFile /etc/ssl/certs/example.com.crt
   SSLCertificateKeyFile /etc/ssl/certs/example.key
   SSLCertificateChainFile /etc/ssl/certs/sf_bundle.crt
</VirtualHost>

I would like everything to redirect to https://example.com/sort-url.php?rt=$1 where I can then determine which page to show based on the variable $1.

Can someone please tell what I need to do? ServerAlias in the and the 301 redirect inside of the haven't worked for me.

Thank you in advance!

Tim

Community
  • 1
  • 1
timfstl
  • 21
  • 5
  • This might help (although it is from https to http): [Apache mod rewrite https to http only for a specific URL](http://stackoverflow.com/questions/10103321/apache-mod-rewrite-https-to-http-only-for-a-specific-url) – Jost Oct 09 '13 at 07:03
  • I'm not sure what to add or do to my code from that. My knowledge of mod rewrite is very limited.... – timfstl Oct 09 '13 at 22:23

2 Answers2

1

Welcome to SO!

If you want EVERYTHING to end up in example.com you could make it your _default_ site. I think you could do it like this:

<VirtualHost _default_:*>
   Redirect permanent / https://example.com
</VirtualHost>

<VirtualHost XXX.XXX.XX.XXX:123>
   ServerName example.com
   DocumentRoot /home/tim/examplepath/
   SSLEngine on
   SSLCertificateFile /etc/ssl/certs/example.com.crt
   SSLCertificateKeyFile /etc/ssl/certs/example.key
   SSLCertificateChainFile /etc/ssl/certs/sf_bundle.crt

   RewriteEngine On
   RewriteBase /
   # Check is HTTPS is used
   RewriteCond %{HTTPS} =off
   RewriteRule ^(.*)$ https://example.com/sort-url.php?rt=$1 [R=301,L]

   # If we have not specified a file to access, redirect to sort-url.php
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ sort-url.php?rt=$1
</VirtualHost>

I must admit this is untested, and I am a bit unsure if you need a VirtualHost for non-SSL connections that redirect to the SSL one.

EDIT
Changed so that the default host just forward to https://example.com to make sure the correct hostname is used.

Qben
  • 2,617
  • 2
  • 24
  • 36
  • Thank you Qben. It didn't like the "RewriteBase /" in the VirtualHost (says only in directory). Are you suggesting I don't need a ? Also, is there supposed to be an "=" in "%{HTTPS} =off"? I'm still troubled by this problem. Will post if I find a fix... – timfstl Oct 09 '13 at 22:27
  • I would say that `RewriteBase` in this case can be removed since it's relative to `DocumentRoot`. Yes, it should say `=off` since you want to do the rewrite if HTTPS is not enabled. Since your `DocumentRoot` is the same path as your `Directory` you should be able to skip it. Basically I moved your stuff from `Directory` into the `VirtualHost`. Could you explain what happen when you access `www.example.com`? – Qben Oct 10 '13 at 05:30
  • When I try to access www.example.com, it takes me here: http://search.dnsassist.verizon.net/ – timfstl Oct 10 '13 at 23:48
  • I tried the code you provided above. When I restarted apache, it informed me that a virtualhost did not exist for *.80. Once I added back a virtualhost for port 80, the site directed me to a "Bad Request" page... Any ideas? I'm completely stumped on this one. – timfstl Oct 10 '13 at 23:49
  • You seem to be using ip based virtual hosts instead of name based. What ports do you have `Listen` statements for? – Qben Oct 11 '13 at 05:57
0

I had to add a CNAME to my DNS records on my Rackspace account. The CNAME is "www.highschoolclothing.com".

Thank you everyone for your help though!

timfstl
  • 21
  • 5
  • Ahh, I kind of assumed your DNS records where up to date. Good thing you found it. You can accept your own answer btw. :-) – Qben Oct 11 '13 at 05:58
  • Thanks Qben. StackOverflow wouldn't let me accept my own answer for 6 hours, but I'm able to now! :) I thought my DNS records were good, too, based on what my SSL check showed. I think I misunderstood a few things (this is my first time setting up a server). I didn't know CNAME could be used for the www issue, since all the examples I saw were a little different. I really do appreciate your help though. – timfstl Oct 11 '13 at 07:25