Doriat Mod_rewrite forum archive

mod_rewrite

How to Mod_Rewrite site powered by php script

I assume you already know what is mod_rewrite, php script and why you need to mod_rewrite your php script. So below is the step by step procedure that will help you do mod_rewrite your script. I have also created a list of mod_rewrite tutorials for specific php scripts. I have to say that this site used to be community of experts in mod_rewrite about two years ago. Now this is a different project, yet some useful info is there. If any of you are willing to dedicate time and energy into further development of such project, i am more than willing to offer hosting space here and developing a forum just for mod_rewrite related quaetions. Feel free to contact me at info [@] doriat.com

So back to the process:

Step 1. Create htaccess file

Simply create new file with a name .htaccess Then write the following line in it:

Code:
RewriteEngine On

Then upload your file either to root directory of your site (if your php script is installed in root directory) or in subdirectory of your site e.g /forums (if your php script is installed in subdirectory).

Step 2. Create mod_rewrite rule.

This is the easiest part of the mod_rewrite process or at least it takes less time.

Choose which of the links you would like to make more SE friendly.

For example with forums you want to mod_rewrite links to forums, posts and probably to user's profiles. With some classifieds scripts or cms you want to mod_rewrite links to profiles, ads, news, articles, polls, reviews and comments. With some photo gallery script you want to mod_rewrite links to albums, categories, top and latest uploads, pictures. One main trick here is - mod_rewrite what comes with much of uniqiue and important content.

So i will illustrate this with phpbb 2. For forums it gives the following link viewforum.php?f=2

Mod_rewrite rules start with the following expression

RewriteRule

So you start:
Code:
RewriteEngine On
RewriteRule

then you add ^

It signifies the start of an url
Code:
RewriteEngine On
RewriteRule ^

then you add expression to which you want to rewrite your link.

For example, you want link to look like forum2.html (why do we keep number? it is very important in phpbb, each forum has its own number. As a rule numbers are very improtant in all php scripts as they give information to script on what content to bring)
Code:
RewriteEngine On
RewriteRule ^forum2.html

!!!However, there can be unlimited amound of such numbers and we cannot create rewriterule for each of them. So we create a universal rule. In mod_rewrite syntax you do in the following way: ([0-9]*) for numbers, ([a-zA-Z]*) for letters and ([a-zA-Z0-9]*) for numbers and letters. This way mod_rewrite will keep value that script requires. Thus, we have:

Code:
RewriteEngine On
RewriteRule ^forum([0-9]*).html

then you add your link viewforum.php?f=2

Code:
RewriteEngine On
RewriteRule ^forum([0-9]*).html viewforum.php?f=2

!!!In order to cover all links viewforum.php?f=Value mod_rewrite uses $n, where n is a consistent number of values.

Code:
RewriteEngine On
RewriteRule ^forum([0-9]*).html viewforum.php?f=$1

then you provide guidance on this rule.

Here is the list of possible guidance options. [N] - next [L] - last: (see also mod_rewrite cheat sheet for more details on guidance) So your code will look like this:

Code:
RewriteEngine On
RewriteRule ^forum([0-9]*).html viewforum.php?f=$1 [L]

So you have created mod_rewrite rule. Next step is...

Step 3. Modify your php script

!!!At this stage you are about to make changes to your php script files. So you need to back up all files that you will change first. Create backup folder on your computer and copy there all files that you will change.!!!

This is the hardest part as each php script is not alike, although most php scripts have similar structures of which i will tell in a moment. At this moment i can say that you need to look into code of your php script and find those parts that are responsible for links.

About Structures of php based scripts:

1. Links are in templates

Some scripts contain all patchs and links in template files, usually these ones are html or tpl files. However, this can be php files as well. Template files as a rule stored in folders called desing or templates.

2. Links are in source files

By source files i mean files in such folders as source, sc, includes, inc, etc. These could also be places where you can find files to modify links. Files that can contain links might be called fucntions.php, source.inc.php,, etc.

3. Links are in files that carry their names

This is the case with phpBB. If you want to mod_rewrite link viewforum.php?f=$1 then you need to look into the following php files:

4. Links are everywhere.

This is very usual case with most of the php based scripts. This is the hardest part in modification. You have to literarly go through all files to find the link.

How do you test this?

First tip. Open the file and do search for name of the link. For example if you want to modify link viewforum.php?f=$1 then look for viewforum.php

Second tip. When you found appropriate code identify the variable that is vital. For example for viewforum.php?f=$1 $1 is vital as it differes all other pages named viewforum.php.

Third tip. Rename the file to whatever you planned in your mod_rewrite rule, save changes and upload your page overwriting back to your server. Then refresh the page and see if link changed. If yes, you are in the right place, if know, start with tip#1

Fourth tip. After you found out that link has changed, press this link and see if it brings you to right page. If it does not that you need to check your .htaccess file rules. Mostlikely you gave your apache module mod_rewrite wrong rule to rewrite.

!!! This article is in development process, i add here as i have time so please be patient and if you have any questions email them to info [@] doriat.com!!!

Common Mod_rewrite Mistakes and Ommissions

In this post i will create a list of common mod_rewrite mistakes or ommissions that i beleive will help you create better mod_rewrite code and will assist you in mod_rewrite of your site. Some of them are related to mod_rewrite code itself, others are related to php and still others to your site code. Here are first two:

Images Problem with mod_rewrite

1) images problem: When creating a mod_rewrite with a desire to have several folders. For example:

Code:
RewriteRule ^(.*)/(.*)/(.*).html$ index.php?id=$1&display=$2&cat=$3 [L]

You face a problem of images not displaying and your styles are not showing correctly. Reason for this is that you create folder with subfolders, while path to images and css files might be relative in your files. For example

Code: < img src=img/sun.gif border=0 >

or

Code:
< css=css/styles.css >

There are two ways out of this situation:

a) You manually change relative path to absolute:

Code:
< img src=/img/sun.gif border=0 >

or

Code:
< css=/css/styles.css >

b) or you do not create folders/subfolders structure. So instead make the following code for .htaccess file:

Code:
RewriteRule ^(.*)_(.*)_(.*).html$ index.php?id=$1&display=$2&cat=$3 [L]

or

Code:
RewriteRule ^(.*)-(.*)-(.*).html$ index.php?id=$1&display=$2&cat=$3 [L]

Sessions Problem with mod_rewrite

2) sessions problem: Some of the premade php scripts have sessions in URLs displaying. For example

Code:
index.php?photo=3&sessionid=20de08e9b9eb696a51e50b165ec9513f

These are the hardest to mod_rewrite for one simple reason. It requires addition php coding to remove such sessions at least for guests. (Actually, removing them for guests is the only thing required in terms of sef. The thing is that search engines, especially google will look into the page index.php?photo=3&sessionid=20de08e9b9eb696a51e50b165ec9513f and page index.php?photo=3

as two different pages with the same content, this is pinalized as duplicate content).

The main way out of this problem is remove sessions for guests. For some scripts like phpBB there are instructions on how to do that, for other there are none. So you need spesialised assistance of php programmer to help you out. From personal experience i have to say that for most premade php scripts it is easy to remove, for others possible :)

If it is not possible to remove session then you need to at least modify your php script not to mess up the site. Make sure the session comes after the url - for example

Code:
index45.html&session=343247234823406320423

instead of

Code:
index45&session=343247234823406320423.html

i hope this helps you out. If you want to share your mistakes, tips feel free to post them here. Here is a resource on sessions, Why You Should Disable PHP's session use_trans_sid

Mod_rewrite

Silk Road Tours | Silk Road | Silk Route Travel | Hotels | Airport Transfer

| Travel Website Design | Africa Tours | Seidenstrasse