Novell is now a part of Micro Focus

Changing a Regular URL into one with a Tilde without Annoying your Users

Novell Cool Solutions: Tip
By Greg Riedesel

Digg This - Slashdot This

Posted: 24 Aug 2005

"Useful tip -- helps clean up homedir URLs."
--Kirk Coombs, Product Specialist

PROBLEM: Mod_edir is designed to allow the serving of web-pages from a user's home directory, no matter what NetWare server the home directory is located. The Apache module userdirs provides this in other areas, but is limited to the local file-system with NetWare. Mod_edir fixes this.

userdirs and mod_edir both need a prefix character in order to tell apache to hook the module. Traditionally this is the tilde character, '~', which is how URLs like http://server/~username/myblog/index.html come about.

But what if you want to provide that functionality, but without the extra character? Such as http://server/username/myblog/index.html and such?

SOLUTION: There are two modules that come to your rescue. Mod_rewrite and mod_proxy. Mod_rewrite allows you to change a character-less URL (http://server/username) into one with a URL (http://server/~username), and mod_proxy allows you to do it without telling the user that you're doing it.

<ifModule mod_rewrite.c>
 <ifModule mod_proxy.c>
    RewriteEngine  on

    # Make the needed exceptions

    RewriteCond    %{REQUEST_URI}         ^.*/icons/.*
    RewriteRule    ^(.*)$                 $1           [PT,L]

    RewriteCond    %{REQUEST_URI}         ^.*/error/.*
    RewriteRule    ^(.*)$                 $1           [PT,L]

    # Make /username work, remember: each paren is a $#, sequentially

    RewriteRule    ^/([a-z0-9]+)$         /$1/         [R]
    RewriteRule    ^/([a-z0-9]+)/$        /~$1/        [PT]
    RewriteRule    ^/([a-z0-9]+)/(.*)$    /~$1/$2      [PT]


The RewriteCond/RewriteRule pairs function like If/Then statements. The two above permit hard-coded directories to be exempted from processing, which is useful. The RewriteRule lines below the two pairs are the meat of the rule processing.

The first rule puts on a trailing backslash, and rewrites the URL in the browser to reflect this.

The second rule takes URLs in the form http://servername/username/ and translates this to include a tilde. The [PT] at the end tells it to proxy the request transparently, so the requesting browser isn't told about the redirection.

The third line handles requests in the form http://servername/username/otherstuff to insert the tilde before the username.

This block has been serving me for over a year with Apache2 and mod_edir, and several months before that with Apache1.3 and mod_hdir+mod_rdir. It works quite well. The beauty of this configuration is that it allows URLs with the tilde to still work, so the purists out there can be happy.

Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions.

© Copyright Micro Focus or one of its affiliates