Archive for October, 2009

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code in your .htaccess file

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This will redirect any requests to http://mysite.com to http://www.mysite.com. There are benefits from doing that:

* It will avoid duplicate content in Google
* It will avoid the possibility of split page rank and/or split link popularity (inbound links).
* It’s nicer, and more consistent.

Please make sure that if your site has already been indexed by Google without the www, this might cause unwanted side effects, like lost of PR. I don’t think this would happen, or in any case it would be a temporary issue (we are doing a permanent redirect, 301, so Google should transfer all rankings to the www site). But anyway, use at your own risk ;) !

Another nice way of writing .htaccess is code above is that you can use it for any website, since it doesn’t include the actual domain name.
Redirecting www to non-www

If you want to do www to non-www, the code is very similar:
in your .htaccess file

RewriteEngine On
RewriteCond %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]

In this case we are explicitly typing the domain name.

I’m sure it is possible to do it in a generic way same as above, but I haven’t had the time to work one out. So remember to change ‘mysite’ with your domain name!, and I’m keeping this post open for all my readers to comment on it and post generic code for www to non-www redirection.

Tags:

Below is example of how to start, stop and restart a windows service programmatically using C#.

Start service

The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs.

public static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// Write code for error log
}
}

Stop service

The following method tries to stop the specified service and it waits until the service is stopped or a timeout occurs.

public static void StopService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
catch
{
// Write code for error log
}
}

Restart service

This method combinates both previous methods. It tries to stop the service (and waits until it’s stopped) then it begins to start the service (and waits until the service is running). The specified timeout is used for both operations together.

public static void RestartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));

service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// Write code for error log
}
}

Tags: