NoPic eZine
  
Home
phpWebSite design aid
phpWebSite Main
phpWebSite Forum 1.x
phpWebSite AT (Rene)
phpWebSite DE,EU
phpWebSite DK (Kenneth)
phpWebSite Community
phpWebSite Manual
phpWebSite SupportForum
phpWebSite Wiki
Impressum/Imprint
Datenschutz/Policies
dc4db eZines
 

    de en

Mod_Rewrite.txt

Using Mod_Rewrite with phpWebSite
by Matthew McNaney
---------------------------------------------------------------------

When writing a module, you may find that some of your links are
getting rather complicated.

For example, say you are writing a module that looks at news
articles. You have link to an article on your home page and it looks
something like the following:

http://www.mysite.com/index.php?module=article&action=view&id=5312

Using mod_rewrite functionality, you could change that link to:

http://www.mysite.com/article5312.html

This is more straight forward, easier for search engines to index, and
much easier to explain over the phone. PhpWebSite can help you achieve
this.

Before you start trying to write modules that take advantage of this,
you will need to make sure your installation of phpWebSite supports
it.

First off, you have to be running Apache. If you are not, it won't
work. There may be solutions for Microsoft IIS and other web servers
but you will have to learn the differences yourself.

Second, you need to configure Apache to use mod_rewrite. Look in your
httpd.conf file for this line:
LoadModule rewrite_module modules/mod_rewrite.so
Add it if it is not there and restart your server.

Third, look in your phpWebSite root directory and you should see a file
named ".htaccess" (note the period). Uncomment all the lines by
removing the pound sign (#) in front of each line.

Finally, you will need to configure phpWebSite to use mod_rewrite. Go
into your config/core/config.php file and change the define to:
define("MOD_REWRITE_ENABLED", TRUE);

Your site should be ready to use mod_rewrite now.

Making your Module use mod_rewrite

The rewrite function is very basic. All it facilitates are simple user
accessible commands. You won't be accessing administrative
functionality using rewrite as it is unnecessary. Pages that can not
be viewed by the general public have no need to use rewrite.

You will need a way to 'catch' the request for viewing an item in your
module. Let's look at our previous example:

http://www.mysite.com/article5312.html

When this is sent to phpWebSite, it is going to go to the article
module and tell it that it wants to view article 5312. It will rewrite
the address to:

http://www.mysite.com/index.php?module=article&id=5312

You are sending two bits of information:
1) what module you are using,
2) what the id should be used.

Since I am only using the mod_rewrite to view items, my index file
assumes that if it only getting an id then this must be a user viewing
a file.

In my index.php file, I may have something like the following.

if (isset($_REQUEST['id']){
  $article = & new Article($_REQUEST['id']);
  $article->view();
}


Keep in mind however that not everyone will use mod_rewrite, so you
should plan accordingly.

if (MOD_REWRITE_ENABLED == TRUE)
   $link = 'article' . $id . '.html';
else
   $link = "index.php?module=article&id=5312";


One more quick note. When phpWebSite uses mod_rewrite, it has to make
sure that relative links in your theme still function. Therefore, the
layout module will insert the following in with your metatags:

<base href="http://www.your_site_address.org/" />

If it doesn't do this, the relative links (i.e.images, javascript,
style sheets) will start looking in directories below the fake address
sent to the page. The only problem with this is that if you output
anything outside of using the Layout module, it will disrupt this
header statement and your style sheets and images may have problems.

To prevent this, make sure that all output is through Layout and that
you catch errors using the Error class.


Copyright © 2006, VbID Verlagsbüro GmbH
pWS modules dcP, dcT, dc4db, Copyright © 2006, VbID Verlagsbüro GmbH
This Site is powered by phpWebSite © The Web Technology Group, Appalachian State University