Archive for October, 2008

URL Layout

URL Layout

Canonical URLs

On some webservers there are more than one URL for a resource. Usually there are canonical URLs (which should be actually used and distributed) and those which are just shortcuts, internal ones, etc. Independed which URL the user supplied with the request he should finally see the canonical one only.

We do an external HTTP redirect for all non-canonical URLs to fix them in the location view of the Browser and for all subsequent requests. In the example ruleset below we replace /~user by the canonical /u/user and fix a missing trailing slash for /u/user.

RewriteRule   ^/~([^/]+)/?(.*)    /u/$1/$2  [R]
RewriteRule   ^/([uge])/([^/]+)$  /$1/$2/   [R]

Canonical Hostnames


RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*){SERVER_PORT}/$1 [L,R]
RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)$1 [L,R]

Moved DocumentRoot

Usually the DocumentRoot of the webserver directly relates to the URL “/”. But often this data is not really of top-level priority, it is perhaps just one entity of a lot of data pools. For instance at our Intranet sites there are /e/www/ (the homepage for WWW), /e/sww/ (the homepage for the Intranet) etc. Now because the data of the DocumentRoot stays at /e/www/ we had to make sure that all inlined images and other stuff inside this data pool work for subsequent requests.

We just redirect the URL / to /e/www/. While is seems trivial it is actually trivial with mod_rewrite, only. Because the typical old mechanisms of URL Aliases (as provides by mod_alias and friends) only used prefix matching. With this you cannot do such a redirection because the DocumentRoot is a prefix of all URLs. With mod_rewrite it is really trivial:

RewriteEngine on
RewriteRule   ^/$  /e/www/  [R]

Trailing Slash Problem

Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, because if you say /~quux/foo instead of /~quux/foo/ then the server searches for a file named foo. And because this file is a directory it complains. Actually is tries to fix it themself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc.

The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect! So, to do this trick we write:

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo$  foo/  [R]

The crazy and lazy can even do the following in the top-level .htaccess file of their homedir. But notice that this creates some processing overhead.

RewriteEngine  on
RewriteBase    /~quux/
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$           $1/  [R]

Webcluster through Homogeneous URL Layout

We want to create a homogenous and consistent URL layout over all WWW servers on a Intranet webcluster, i.e. all URLs (per definition server local and thus server dependent!) become actually server independed! What we want is to give the WWW namespace a consistent server-independend layout: no URL should have to include any physically correct target server. The cluster itself should drive us automatically to the physical target host.

First, the knowledge of the target servers come from (distributed) external maps which contain information where our users, groups and entities stay. The have the form

user1  server_of_user1
user2  server_of_user2
:      :

We put them into files Second we need to instruct all servers to redirect URLs of the forms




when the URL is not locally valid to a server. The following ruleset does this for us by the help of the map files (assuming that server0 is a default server which will be used if a user has no entry in the map):

RewriteEngine on

RewriteMap      user-to-host   txt:/path/to/map.user-to-host
RewriteMap     group-to-host   txt:/path/to/
RewriteMap    entity-to-host   txt:/path/to/map.entity-to-host

RewriteRule   ^/u/([^/]+)/?(.*)   http://${user-to-host:$1|server0}/u/$1/$2
RewriteRule   ^/g/([^/]+)/?(.*)  http://${group-to-host:$1|server0}/g/$1/$2
RewriteRule   ^/e/([^/]+)/?(.*) http://${entity-to-host:$1|server0}/e/$1/$2

RewriteRule   ^/([uge])/([^/]+)/?$          /$1/$2/.www/
RewriteRule   ^/([uge])/([^/]+)/([^.]+.+)   /$1/$2/.www/$3\

October 20, 2008 at 2:40 pm Leave a comment

The Apache configuration htpasswd, md5, mod_rewrite,

The Apache configuration file httpd.conf contains settings applied to all hosted domains, and also domain specific settings (settings contained in <VirtualHost> tags). Of course you don’t want your users to edit the httpd.conf file, but fortunately they can do site specific settings themselves by allowing them to use .htaccess files.

It’s easy…

A .htaccess file (note the dot in front of the filename) is a plain text file with settings in it. It can be placed at the root of a website or any sub directory. The settings in it will apply to the current and all sub directories in the current directory.

First of all, you must set the AllowOverride directive in httpd.conf. AllowOverrride controls which types of directives that are allowed in .htaccess files.:

<Directory “/usr/local/www/”>
AllowOverride All

Add a section like the above for each of the domains you want to allow using .htaccess.

Remember to restart Apache to make the changes take effect.

The following sections of this page, describes som examples of using .htaccess files.

Note: Instruct your users to use ASCII mode when uploading .htaccess files. If BINARY mode is used the file will NOT work.
Password protection using a password file

This section describes how to protect all or part of a website against unauthorized access. The content of the .htaccess file:

AuthUserFile /usr/local/www/secure_directory/.htpasswd
AuthGroupFile /dev/null
AuthName “My protected site”
AuthType Basic

require valid-user

In the first line, replace bolded text with the path to your own protected area. In the third line, replace bolded text with a text of your choice. This text will appear in the login boks.

You may need to CHMOD the .htaccess file to 644 or (RW-R–R–). This makes the file usable by the server.

Create a directory just above your website root. This is where you will create your password file, and we don’t want this in a public directory.

For example, my password file is in /usr/local/www/secure_directory/

Now, you need to create the .htpasswd file. You can do it manually by putting each users username an password into it:


Each line must end with a line break, and there must be an empty line at the end of the file.

Another way of creating the password file is by using the htpasswd command, but this requires ssh access to the server.

htpasswd -c /usr/local/www/secure_directory/.htpasswd pas
New password:
Re-type new password:
Adding password for user pas

Next time you want to create a user, ommit the -c option (this is important to remember, or you will override the existing users/passwords in the file). For security reasons, passwords do not show op on the screen as you type.

This example shows how to change the password for an existing user (bold text is all in one line):

htpasswd -b /usr/local/www/secure_directory/.htpasswd pas fff
Updating password for user pas

October 20, 2008 at 2:34 pm Leave a comment

function html to text

function htmltotext($html)
$tags = array (
0 => ‘~<h[123][^>]+>~si’,
1 => ‘~<h[456][^>]+>~si’,
2 => ‘~<table[^>]+>~si’,
3 => ‘~<tr[^>]+>~si’,
4 => ‘~<li[^>]+>~si’,
5 => ‘~<br[^>]+>~si’,
6 => ‘~<p[^>]+>~si’,
7 => ‘~<div[^>]+>~si’,
$html = preg_replace($tags,”\n”,$html);
$html = preg_replace(‘~</t(d|h)>\s*<t(d|h)[^>]+>~si’,’ – ‘,$html);
$html = preg_replace(‘~<[^>]+>~s’,”,$html);

$html = preg_replace(‘~ +~s’,’ ‘,$html);
$html = preg_replace(‘~^\s+~m’,”,$html);
$html = preg_replace(‘~\s+$~m’,”,$html);

$html = preg_replace(‘~\n+~s’,”\n”,$html);
return $html;

October 18, 2008 at 10:12 am Leave a comment

function error php

function error( $message )

echo ‘<div style=”color:red;font-weight:bold;font-size:12pt;font-family:monospace”>’.

} // eof error()

October 18, 2008 at 10:09 am Leave a comment

Function php infoboxes with cookie

function infoboxes()
if( isset( $_COOKIE[‘infoboxes’] ))
if( $this->debug) echo $_COOKIE[‘infoboxes’];

$this->showinfoboxex = ( $_COOKIE[‘infoboxes’] == ‘ON’ );

October 18, 2008 at 10:08 am Leave a comment

function checkitems combolbox list c-sharp

public void CheckItem(string sItem)
for (int i = 0; i < cblPinakes.Items.Count; i++)

if (cblPinakes.Items[i].ToString() == sItem)
cblPinakes.SetItemChecked(i, true);
cblPinakes.SetSelected(i, true);

October 18, 2008 at 10:05 am Leave a comment

Function c# Load tables from sql 2000 -2005

public void Load_Tables()

System.Data.SqlClient.SqlCommand sqlcmdselect = new SqlCommand();
sqlcmdselect.CommandText = “select name, ‘0’ as see  from sysobjects where xtype=’U'” ;
sqlcmdselect.Connection = MSSqlcon;
System.Data.SqlClient.SqlDataAdapter sqlDataAdapter_kos = new SqlDataAdapter();
sqlDataAdapter_kos.SelectCommand = sqlcmdselect;


DataRow[] row_select = dataSetPinakes.Tables[0].Select();
for (int bb = 0; bb < row_select.Length; bb++)

object obj = row_select[bb][0].ToString();


DataTable dt = dataSetPinakes.Tables[0].Copy();

DataRow[] rowsel = dt.Select();
for (int ii = 0; ii < rowsel.Length; ii++)
if (rowsel[ii][1].ToString() == “1”)

catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }


October 18, 2008 at 10:03 am Leave a comment

isIdentity sql query

select COLUMNPROPERTY(object_id(‘tmp’),’id1′,’isIdentity’)
sp_configure ‘allow update’, 1
reconfigure with override
update syscolumns set colstat = colstat – 1
where id = object_id(‘tmp’)
and name = ‘id1’
exec sp_configure ‘allow update’, 0
reconfigure with override

October 18, 2008 at 10:02 am Leave a comment

Function c# run sql destination

public void runsqldestination(string query)

string sqlupd = query;

SqlCommand cmd6 = new SqlCommand(sqlupd, MSSqlcon2);
cmd6.CommandTimeout = 99920;



{ MessageBox.Show(” Error ! “); }

October 18, 2008 at 9:59 am Leave a comment

Convert dataset to sql query insert

public void GetDataset(DataSet ds, string TableName)
string squeryInsert = “insert into ” + TableName + ” (“;

for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
if (i == 0)
squeryInsert = squeryInsert + ds.Tables[0].Columns[i].ToString();
squeryInsert = squeryInsert + “,” + ds.Tables[0].Columns[i].ToString();

squeryInsert = squeryInsert + “) values ( “;

DataRow[] row1 = ds.Tables[0].Select();

string squery = “”;
for (int ii = 0; ii < row1.Length; ii++)//pernoume ta rows
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)// pernoume ta column
if (i == 0)
squery = squery + “N'” + row1[ii][i].ToString().Replace(“‘”, “””).Replace(@”\”, @”\\”) + “‘”;
squery = squery + “,N” + “‘” + row1[ii][i].ToString().Replace(“‘”, “””).Replace(@”\”, @”\\”) + “‘”;

squery = squeryInsert + squery + ” )”;


lblProgressBar.Text = “Progess  ” + TableName.ToUpper() + ” :”;
progressBar1.Maximum = row1.Length;
progressBar1.Minimum = 0;
progressBar1.Value = ii;

squery = “”;



October 18, 2008 at 9:58 am Leave a comment

October 2008