Handling multiple SSL sites on a single IP address using Apache mod_rewrite

Category : Programming, Technology, Web Design, Work

Recently at work we needed to come up with a way to have multiple sites using SSL on a single IP address. Each site used a different sub domain.

Normally with Apache, each site set up with SSL needs it’s own unique IP address, however by adding some clever internal redirect code into the Apache config files, it is possible to achieve exactly what we needed. I’m posting this here in case anyone else has the same problem and happens to come across this post!

 

This is done in the conf/httpd-vhosts.conf file.

One thing to note is that any other vhosts setup already for specific subdomains will continue to function as normal and will bypass this code entirely.

1. Firstly, the server must be assigned an unused SSL IP address which will handle all the site subdomains. In the example below, this IP address is: 123.45.67.89

2. Insert virtual hosts containing the rewrite code, into Apache

Below is the apache config code which achieves this. This can be lifted and added to a new server when needed. I have added some comments to explain what it is doing

# first we add a vhost for all port 80 traffic. This is to catch http://anysubdomain.yoursite.co.uk
<VirtualHost *:80>
ServerAdmin support@yoursite.co.uk

#the document root needs to be moved up one folder so we can then specify the subfolders for the individual sites
#in the main config file, the same change should be been made (i.e.if the server root was g:/inetpub/wwwroot, change to g:/inetpub)
DocumentRoot “g:/inetpub/”

#turn on the rewrite code (make sure mod_rewrite module is enabled in main httpd.conf)
RewriteEngine on

# this first line is important as it is the one that stops it recursively redirecting in an infinite loop.
# The condition is basically saying “do not do this if wwwroot is already in the path”
RewriteCond $1 !^wwwroot

#this was needed to stop it redirecting the PHP paths, which caused everything to break
#what is needed here may differ slightly depending on the PHP path variables setup, but this should cover most, if not all cases
# or may not be needed at all – depends on your config
RewriteCond $1 !/php5/
RewriteCond $1 !/php/

# this is the condition which says foranything.yoursite.co.uk , apply the rewrite rule
RewriteCond %{HTTP_HOST} ^([^.]+)\.yoursite\.co\.uk

#and this actually applies the rule. It prepends the path with wwwroot, so apache knows where to find the relevant files.
#the url shown in the browser does not change
RewriteRule (.*) /wwwroot/%1/$1 [L]

</VirtualHost>

#We now create a virtual host for the IP we have assigned to the server, for SSL connections.
# Other than the virtual host declaration, the code is exactly the same as above, so no need to comment further.
<VirtualHost 123.45.67.89:443>
ServerAdmin yourname@yoursite.co.uk

# the paths to your SSL certs etc – change as appropriate
SSLEngine On
SSLCertificateFile conf/ssl/server.crt
SSLCertificateKeyFile conf/ssl/server.key

DocumentRoot “g:/inetpub/”
RewriteEngine on

RewriteCond $1 !^wwwroot
RewriteCond $1 !/php5/
RewriteCond $1 !/php/
RewriteCond %{HTTP_HOST} ^([^.]+)\.yoursite\.co\.uk
RewriteRule (.*) /wwwroot/%1/$1 [L]

</VirtualHost>

Sending Emails with C# and .NET

Category : Programming

Sending emails from an aspx web page couldn’t be more simple with C# and .NETHeres the code which will send the email. I’ll leave it up to you to figure out how to fill the various values in from a form, as that bit is pretty straightforward (I would hope!)

System.Web.Mail.MailMessage m = new System.Web.Mail.MailMessage();
m.From=”fromaddress@here.com”;
m.To=”toaddress@here.com”;
m.Subject = “put your subject here”;
m.Body=”put your body here”;
System.Web.Mail.SmtpMail.Send(m);

to add an attachment…

System.Web.Mail.MailMessage a = new System.Web.Mail.MailAttachment(Server.MapPath(“PATHTOFILEHERE”));
m.Attachments.Add(a);

Uploading Files with ASP.Net (C#)

Category : Programming

I needed to upload some files recently for the support portal I am working on at work. This article was extremely useful so I thought I would pass it on!http://www.aspheute.com/english/20000802.asp

Working with XML in .NET – Crib sheet

Category : Programming

Here are some basic tips on handling XML in .NET. I hope it is of use to somebody! Hell, if it isn’t, it will be of use to me!This code is VB .NET, but the principle is the same for C#. It’s only the syntax that changes slightly.

First things first

Firstly, you need to add a reference to System.XML to your project.

Working with the XML file

[u”>Declare the variable:[/u”>

Private MyXMLDoc As New System.Xml.XmlDocument()

[u”>Load the file into memory:[/u”>

MyXMLDoc.Load(FilePath)

Working within the XML file on nodes, and groups of nodes

[u”>Declaring a node variable[/u”>

Dim Node As System.Xml.XmlNode

[u”>Get the main DOCUMENT element of an XML file[/u”>

Document = MyXMLDoc.DocumentElement

[u”>Get a list of nodes by a path in the XML flle[/u”>

Dim NodesList As System.Xml.XmlNodeList
NodesList = Document.SelectNodes("path/to/node")

eg. Using this XML input (assuming we are already inside the document element)…

<path>
<to>
<node>node one</node>
<node>node two</node>
<node>node three</node>
</to>
</path>

NodesList effectively becomes…
<node>node one</node>
<node>node two</node>
<node>node three</node>

[u”>Get a node from the list[/u”>

Dim Node As System.Xml.XmlNode
Node= NodesList.Item(Index Integer)

Following on from our example above, NodesList.Item[1″> would return:
<node>nodetwo</node>

Node values and attributes

[u”>Getting the value out of the node[/u”>

Value = Node.InnerText

eg. for the Node <field>Name</field>, the above would return the value "Name"

[u”>Getting Node attribute values[/u”>

Node.Attributes(Index Integer).Value

e.g. on the node…
<field id="777" type="boolean">Jam</field>
Node.Attributes(1).Value would give the value – "boolean"

Editing XML Files

Editing XML files is really easy. When you create an instance of a node, this references back to the node in the original XML document. That means, any changes made on the node are reflected back into the original document.

Obviously, these changes are just made in memory until they are committed to a physical file. This is easy. You simply need to call the “Save” function on the XML document object.

e.g.
MyXMLDoc.Save(FilePath)

That concludes this little crib sheet for now. It should be enough to introduce you to the basic concepts. The rest you can figure out for yourself!

Free alternative to Visual Studio .NET

Category : Programming

Anyone out there wanting to learn .NET (like me at the moment) may be daunted by the price of Microsofts lovely .NET development studio (curiosly named “Microsoft Visual Studio .NET”), should try SharpDevelopIt is a free, open source alternative to MVS.NET. I’ve just had a quick play with it and it seems OK. Looks very much like the Microsoft product.

It lacks a few of Microsofts particularly clever features, like automatic code checking and correction, but then, it’s free, so what do you expect?!

I already own Visual Studio, and why ride a donkey when you own a horse?. So sadly, I won’t be doing much work with it, so can’t really tell you what it’s like, but, it does seem to come well reccommended.

Website: http://www.icsharpcode.net/opensource/sd/

I’m going to learn .Net

Category : Programming

I’ve decided that I need to become more knowledgable in the ways of the .NET…

I have shaken the dust sitting on the portion of my hard drive containing Visual Studio, and I am going to endeavour to delve into the world of .NET programming.

I already have a project in mind. I am going to write a tool which will allow the edition of the XML meta data file which is used to generate databases for the software we sell at work (TeamLink Evolution)

I will try and post bits as I learn them in case they come in handy to anyone (and so I can remember for future!!)

My first .NET Application – PMimer64

Category : Programming

Hurrah, been playing with .NET for half an hour and I have already developed my first application.

PMimer64 is a basic application which encodes / decodes text into Base 64.There are already applications out there that do this, but I wanted to add a handy little feature which copies the resultant text straight into the clipboard automatically.

So I did.

If anyone happens to want this simple little application, please feel free to email me at: dotnet@maybetomorrow.co.uk