timfstl
timfstl

Reputation: 21

Redirect www to non-www on https site using mod rewrite, VirtualHost, and/or Directory

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

Upvotes: 2

Views: 2192

Answers (2)

timfstl
timfstl

Reputation: 21

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!

Upvotes: 0

Qben
Qben

Reputation: 2623

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.

Upvotes: 1

Related Questions