How To Make Short URLs
This article will show you a way of making short URLs from long ones. The method uses
ProLinkzTM
to do the redirection and the .htaccess file to expand the short URL to one ProLinkz can use.
This is an example long URL of the kind this article
addresses:
http://example.com/cgi-bin/prolinkz/pl.cgi?code
This is an example short URL that can be published instead:
http://example.com/code
("code" in the above URL is used by the system to determine
the destination URL for the browser redirect. It's identical
to the code at the end of the ProLinkz URL.)
The .htaccess edit (shown below) extracts the code from the
URL and sends it to ProLinkz.
ProLinkzTM
ProLinkz redirects the browser to the correct
destination URL and counts the occurrences. It is
excellent tracking software.
(Surveys are great. But I think sometimes people answer the way they think things are, or the way they feel things should be. A click counter like ProLinkz measures actual action, independent of thoughts or feelings.)
ProLinkz comes with installation instructions. See
the ProLinkz web site.
Set up a
redirect code with the ProLinkz control panel. When you
are ready to publish the short URL link, publish the URL
to your domain followed by a slash and the redirect code.
But before you publish the short URL link, you'll need to
insert some lines into the .htaccess file.
The .htaccess File
While editing the .htaccess file is easy, just copy the generated lines and
paste, there are several important notes in this article
that need to be understood. And the instructions themselves
are rather long because each line being pasted into the
.htaccess file is addressed.
More information about the Apache module mod_rewrite (which
is used in this "short URL" implementation) can be found at
the Apache Software Foundation. Two articles you may wish
to read are "Module mod_rewrite; URL Rewriting Engine" and
"URL Rewriting Guide"
Seven or eight lines will need to be added to the .htaccess
file, the number of lines depending on whether or not you
have specified an error document handler in the file.
Use the generator below to generate the lines.
If you already have an .htaccess file in the document root
directory of your server, you can paste the new lines at
the end of the file.
If you do not have an .htaccess file, create one in the
document root directory.
(The document root directory is the directory where your
domain's index file is at, the file containing the web page
visitors see when they type only your domain name into
their browsers.)
Important note #1:
Some servers are configured so the .htaccess file does
not appear in FTP file listings.
Determine whether or not there is a .htaccess in the
document root directory before you create a new one
and overwrite what might already be there.
To determine whether or not a .htaccess file is in
the directory, try to download the file by manually
specifying .htaccess as the name of the file to
download. If the file is in the directory, the FTP
program should be able to find and download it.
The .htaccess edit will cause the server to use the Apache
module mod_rewrite to redirect URLs containing a ProLinkz
code to the ProLinkz link handling script.
Important note #2:
Some hosting companies configure their servers so their
clients can not use the Apache module mod_rewrite.
>>> If yours is configured that way, attempting to use
the Apache module mod_rewrite causes an Internal Server
Error for any and all pages your visitors might try to
access! <<<
Therefore, either find out from your hosting company
whether or not you can use Apache module mod_rewrite,
or test the server yourself.
To test the server, create a subdirectory in the
document root. Let's call it "test" for example.
Upload a .htaccess file into the "test" subdirectory
that contains this line:
RewriteEngine on
Also upload a file named index.html into the "test"
subdirectory. It doesn't matter what this index.html
file contains so long as it's plain text.
Now, use your browser to access that directory. Access
it by typing
http://example.com/test/
into the browser's address bar (replacing example.com
with your own domain name).
If you get an Internal Server Error (or an error page
indicating something is awry), then you won't be able
to implement the method described in this article with
that hosting account unless the hosting company
makes the Apache module mod_rewrite available.
If things seem normal, the index.html file loads into
the browser, then your account is able to use Apache
module mod_rewrite.
After the test, remove the files from the "test"
subdirectory, then remove the subdirectory itself.
Now that you know your hosting account can use Apache module
mod_rewrite, let's edit the .htaccess file for the document
root.
Load the .htaccess file into a plain text word processor
like NotePad or BBEdit.
Generate the lines with the generator and add them to the .htaccess file.
Here is a line-by-line explanation.
Line 1 turns the rewrite engine on. The rewrite engine is
in the Apache module mod_rewrite, which your hosting account
must be able to use. See "Important note #2", above.
Line 2 tells the rewrite engine that the base of URIs is
your document root.
Line 3 is a condition line, specifying that the URI must
not be blank, will never do the redirect if it is blank,
like it would be with URL http://example.com/ for example.
(If any condition lines fail, the redirect won't happen.)
Line 4 is another condition line, specifying that the URI
may not contain a "/" character after a letter or number.
This aborts the redirect for http://example.com/file/ URLs.
The [NC] at the end of the line tells the engine that no
case differentiation shall take place (an "A" to be
considered equal to an "a").
If you answered "Yes" to the question about whether or not your current .htaccess file
contains an error document line, then line 5
aborts the redirect if the URI
contains a period followed by a letter or
number (case-insensitive). For example,
http://example.com/page.html won't redirect.
(If you answered "No", then the generator will not have generated that line.)
The third line from the bottom is a condition line saying that the file name being
requested (which would be the code following the last "/"
in URL http://example.com/code) must not be the name of an
existing file.
The second line from the bottom is a condition line saying that the file name being
requested must not be the name of an existing directory.
If all of those condition lines pass, then the bottom line is
executed. This is where the redirect happens.
When the generated lines are pasted into the .htaccess file,
upload it and test it.
Important note #3:
Do not delay the testing!
When the .htaccess file is uploaded, the effect is
immediate.
If something is awry with the .htaccess file, your
site visitors may receive an Internal Server Error
or may be redirected to a page they shouldn't be
redirected to or may experience page redirects in
an infinite loop.
Just be prepared to delete the .htaccess file from
your server if something like that shows up in your
testing. Delete from the server, then review and
correct the copy you have on your computer. Once
corrected, re-upload and re-test.
Test a URL on your server:
-
To a page that exists.
-
To a page that doesn't exist.
-
With no page name (http://example.com/)
-
With a valid ProLinkz code, to determine that
the short URL works like it should.
-
To a web page in another directory (if you
have documents in other directories) that
exists, and to one that doesn't exist.
If those test okay, the modified .htaccess file is working.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©Copyright 2005 Bontrager Connection, LLC Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.