PHP Classes

File: hn_urlrewrite_readme.txt

Recommend this page to a friend!
  Classes of Horst Nogajski  >  HN URL Rewrite  >  hn_urlrewrite_readme.txt  >  Download  
File: hn_urlrewrite_readme.txt
Role: Documentation
Content type: text/plain
Description: readme
Class: HN URL Rewrite
Emulate request URL rewriting and redirection
Author: By
Last change: updated for class version 3.1
preface added
Date: 17 years ago
Size: 9,155 bytes


Class file image Download
PHP-Class:  hn_urlRewrite
Author:     Horst Nogajski
Version:    3.1
Released:   19-Sep-2004


Content of this readme:

1) What's new in Version 2.x / 3.x
2) What the class does
3) What the class needs
4) How to configure the webserver
5) How to rewrite your URLs
6) How the redirect script works
7) Encoding
8) Filelist, examples
9) ToDo's



My first intention for writing this class (version 1), was the wish for having
a little tool which allows easy rewriting some (defined) URL's, so that they are
more friendly for search engines and a bit unfriendlier for hackers.

When rewriting it to version 2, I've thought that it would be more comfortable
to let the class rewrite all (php) URL's without to have them register into an array.

But this causes more trouble than good and comfortable results. It goes slightly
a bit out of hand. (see Apaches htaccess protection in point 1) )

Therefore the next version, 3.2, will be configured per default to rewrite only
registered URLs.

But first I want to think carefully about some points:
- adding rewrite for other filetypes than php? (e.g. images: gif, jpeg, png)
- adding more tags to RegExp-SearchPattern: <link|<table|<tr|<td?
- also (optionally) encoding the path in URLs, not only the params?
- adding an extension which scans the whole side and parse all htaccess-files
  to automatically avoid the security hole? And then, what's about directory-directives
  written in apaches httpd.conf? Can we fetch them?
- when rewriting all URL's, including images etc., every request forces a call to
  the rewrite-script. How much is the slow down?

If you have any suggestion to this or other points of HN URL Rewrite, please mail me.


1) What's new in Version 2.x / 3.x

ATTENTION! SECURITY HOLE IN version 2.x & 3.0:
I've experienced that, if one use rewriting all URLs and there is also
an URL which refers to a htaccess-password protected script, the class
bypasses Apaches protection!

So I've added a rewrite_protected_scripts array to configuration.
This is used like the registered_scripts array but exclude its scripts from rewriting.

If you use the registered_scripts array you don't need it.

BUT if you use the DEFAULT MODE (rewriting all URLs) and you _HAVE_


Since 3.0 it uses a new RegExp-Searchpattern which allows to rewrite all tags
at once. It's much faster now!


Per default, the class rewrites all URLs which refers to the own serverhost
and where the extension is 'php', if no array with registered scriptnames is defined.

It rewrites the URL links (<a href... </a>),
the IMG links (<img src),
and NEW in 3.0: the AREA links in image maps (<map> <area href... </map>),


The links to rewrite can either relative URIs, absolute URIs, or complete URLs
with scheme, hostname, path. Relative parent-links (../) in relative and absolute
URIs are also resolved (since 2.0)!


Per default it rewrites all tags, but optionally you can disable each of the
tag-groups in configuration with new boolean config-vars: nourls, nomaps, noimgs.

Therefore in 3.0 the three string-rewriting methods from 2.0 has changed to only one:
It return the rewritten string, but do not store it in classes scope.


The class detect complete URLs with and without 'www.' in hostname.
Optionally you can define an array with additional hostnames e.g. if you have
the same site reachable by different top level domains (.com + .de)


Since 2.0, registered scripts are stored in array as values, not as keys anymore!
The param names are not stored in array. They will encoded too.
So their count and order doesn't matter anymore.


Errorhandling is done! (3.0)
Now it fixes the minor Errors to guarantee users can reach pages, e.g. at the price
of not rewritten URLs. Have a look to the function at the end of the class.


(3.0) Also fixed some minor glitches:
- ? removed from QUERY_STRING.
- now it uses is_file() instead of file_exists() by resolving pathes and checking
  if files are available on local system. (file_exists return TRUE on directories also).
  Now it resolves 100% of all pathnames, ;-)
- Have cleaned up syntax for better consistency.


2) What the class does

This class can emulate a page request URL rewriting and redirection
specified within the same PHP script.

It can a PHP script handle a request with an URI like this


when it is requested a page with an URI like this:


or, optionally with strong encoding, to with an URI like this


The request redirection emulation is done by rewriting some request variables
that contain request paths, like SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF,

It can parse and change the output of the current page script to rewrite
the URLs links (also in image maps) or images in the page with absolute URLs,
relative URIs and URLs that contain scheme and host (

There are two methods to do rewriting:
First is by using PHP buffering support. Optionally, the class
can compress the processed page output to serve the page in less time if
the user browser supports compression.
Second is, if you cannot (or don't) want use the buffering support,
using a string-rewrite method.


3) What the class needs:

Per default, all URL links with file-ending .php which refers
to the own server will be rewritten.

Optionally you can register scriptnames in an array, if you
want rewrite only specific URL's. The URL must be defined in
absolute style: '/dir1/subdir/filename.php', without scheme and host!


4) How to configure the webserver

In the root directory you need a htaccess file with an entry like:

 	<Files goto>
 		ForceType application/x-httpd-php

where 'goto' is the name of the rewrite-script, which resides also
in the root dir. In class configuration you have to register this
scriptname also in constant named: URL_REWRITE_SCRIPT, but here
you must add the leading slash: define('URL_REWRITE_SCRIPT','/got');

This forces apache to call the rewriter script for every request,
beginning with /goto.


5) How to rewrite your URLs

After a class instance is build,

	$rewrite =&  new hn_rewrite();

you can use the buffer-methods to catch a whole page at once:


	... all your html output here ...

	$rewrite->send_page();      // optionally it can gzip compress the output, when passing TRUE to the function!
or: $string = $rewrite->page;   // if you don't want direct output


you can use the string rewrite method, if you allready have the
html-output present as string:

	$string = $rewrite->string_rewrite($string);

The method return the rewritten string, but do not store it
in classes scope, so you cannot use the send_page()-method here.
(If you want use it, you can do with an extra step:
	$rewrite->page = $string;
	$rewrite->send_page(TRUE);      // optionally it can gzip compress the output, when passing TRUE to the function!
 But keep in mind that the string must contain the complete HTML-page,
 with doctype, html, header and body tags.)


6) How the redirect script works

The redirect script calls one method of the class:


		[ $param is the pathinfo from the request as it comes from apache,
         (stored in $_SERVER['PATH_INFO']) ]

The request redirection emulation is done by rewriting some request variables
that contain request paths, like SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF,

Also this method writes the decoded params to the superglobal array $_GET,
and changes the current working directory to the requested scripts path.


7) Encoding

In standard use, the class base64_encodes all params, but optionally it
can use a strong encoding which makes decoding for hackers impossible.
It also can use the params in plain text, but this is not recommended!
Because then you have to take care about that there are no slashes (/)
in your param names or param values! This would fool the redirection script,
so that it cannot resolve the correct path!


8) Filelist


Copy all (example)-files _with_ specified subdirs to your
webservers root directory and configure the webserver as
described above!


9) ToDo's

- See Preface!


For more information send a message to info at phpclasses dot org.