mod_rewrite tutorial and multi ladders

mod_rewrite tutorial and multi laddersThis is a tutorial on the Apache module 'mod rewrite' and how to use it to run multi ladders under squash ladder PHP.

You might also find 'A brief tutorial on mod_rewrite and how to get Googlifier installed on PHPNuke v7.5' of interest too!

NB: If you just want to run multi ladders without understanding how it works just look at the first and last code boxes on this page and copy the relevant settings to your ladder installation.

In order to run multiple ladders you need to create a set of tables for EACH extra ladder you want to set up. To do this you need to run the installer script again. I advise you to backup (export with data) your previous tables just in case anything goes wrong with the installer. When you run the installer a second or subsequent time make sure you DO NOT use the same ladder prefix as any of your existing ladders as it could corrupt your db. You should be able to run the installer as many times as you like with new ladder prefixes to set up new ladders. Please only run the installer relevant to your installed version/build no. If you have an older version running you will need to upgarde your code and db to cope with a newer version installer.

Mod_rewrite runs as a module under the Apache webserver. My host runs Red Hat Linux so I'm not sure how to accomplish the same thing under a Windows server. Furthermore I'm NO EXPERT at this so my explanation is perhaps simplistic or even a bit wrong but it does work!!

Multi ladders work by running the ladder code in the root (or base) or your installation but that code is accessed by a URL that contains the other ladder abbreviations. e.g. squash-ladder.net/demo/ passes the ladder value 'demo' to the code located in squash-ladder.net/ and squash-ladder.net/mens/ passes the ladder value 'mens' to the code located in squash-ladder.net/

This means that each ladder can have its own URL to access its own individual customised ladder. That was how I wanted the ladder to operate - I didn't want people to have to login from a generic ladder page. There is however the option to have a default ladder. This means that if the default ladder is 'demo' and the URL is just squash-ladder.net/ then it automatically forwards to squash-ladder.net/demo/

So how does the ladder code in the root of your installation work when the URL is accessing a sub directory ? Mod rewrite takes the accessing URL and uses regular expressions to swap out information in the URL with other information. The new URL is then what is used to find the required page. The code to run this sits in a file called .htaccess which needs to be in the root of your installation. The values I use work for me but I can't guarantee they will for you. You need to ensure that you have the 3 rewrite rules present for EACH ladder you are running.

Here is the sample .htaccess:

RewriteEngine On

RewriteRule ^demo$ http://your.path/demo/index.php [R,QSA,L]
RewriteRule ^demo/$ index.php?eel=demo [QSA,L]
RewriteRule ^demo/(.*)$ $1?eel=demo [QSA,L]

RewriteEngine On

First line turns the engine on.

RewriteRule ^demo$ http://your.path/demo/index.php [R,QSA,L]

In the second line the ^ means the start of the URL starting with this directory. This is important as no matter what path you use to get to your root installation it is only subdirectories that will be affected. The $ means 'end of url'. So... We are looking for a URL your.path/demo and this redirects us to your.path/demo/index.php. There is another way of getting demo to redirect to demo/ and for that to reach demo.index.php but for a couple of reasons I have to use the above method. The R in square brackets means redirect. This makes the browser 'see' the redirect rather than it being done behind the scenes. Here we actually want to see in the browser the URL changing to your.path/demo/index.php. This is then dealt with in the fourth line (see below).

RewriteRule ^demo/$ index.php?eel=demo [QSA,L]

Third line does much the same as the previous line but it redirects your.path/demo/ to your.path/index.php. So we are changing a subdirectory URL to the index.php file in the root. The ?eel=demo bit passes a variable to the script to let it know which ladder we are wanting to access. Here you will see that there is no R in the brackets. This is because we want the browser to only display your.path/demo/ even though the file we are really accessing is your.path/demo/index.php. The L in brackets means this is the last rule we want to process in this URL.

RewriteRule ^demo/(.*)$ $1?eel=demo [QSA,L]

The fourth and final line looks for anything at all in the your.path/demo/ subdirectory and redirects to your.path/. (.*) means look for 0 or more occurrences of any character. We are also choosing to keep this redirect hidden. So for instance your.path/demo/rankings.php gets changed to your.path/rankings.php?eel=demo

The QSA bit in brackets also appends any GET variables in the URL so your.path/demo/rankings.php?p=22 gets changed to your.path/rankings.php?eel=demo&p=22

In essence, anything in a subdirectory gets redirected 'behind the scenes' to the root of the installation. Any data passed by the GET method in the URL is passed and the value eel=demo is appended to the URL. We can therefore run multiple ladders off the same single code installation.

Only 2 things left to do though... You need to set 2 values in the include/connect.php file.

Look for:

/************************************************************************/
// User Settings: Please alter these to meet your requirements
/************************************************************************/
$type="multi";

$root="http://squash-ladder.net/";

$default = "demo";

Set the variable $type to "multi" and set the variable $default to your ladder abbreviation.

That's it. Not sure I've explained it terribly clearly but do post here if you need more help!!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

ok

Hi Matt, I read this while ago and my head is still spinning LOL. Step by step shouldnt be too bad, but I do have one question. I didn't see anything where you actually create a ladder within the application itself. If I understand it right, the 2 steps are create the code in the htaccess file and just one time set the connect file to multi mode.

So at anytime I want to add a new ladder just add it to htaccess? Do any users or admins transfer over or just the default admin. I can have different inormation pages and such?

Not sure if i'm going to get around to trying this tonite, I imagine I can get some answers to these questions this way.

You can use the install

You can use the install script (same one as you used to create first ladder) to install more ladders. It checks and only installs the necessary extra tables. The .htaccess file gets automatically updated to accommodate new ladders so you shouldn't need to touch that unless you are on windows in which case you have to manually edit it. Actually in the interim version 0.9.x I have implemented a far more sensible approach and you just have 1 static .htaccess file that works on all ladders without the need to update for each new ladder.

mod_rewrite tutorial and multi ladders

Hi Teraforce,

Sorry - major oversight on my part :oops: You do need to set up new tables for each new ladder. I have ammended the tutorial above :lol:

Matt

Multiple Boards not working?

I got it up and running in single mode - now I'm trying to run a couple - so I made the changes per the above - ran the install again and now I just get "url not found" errors.

When I look at phpinfo it says the mod_rewrite module is loaded - but it doesn't look to me like the rewrite is happening.

logfiles just say "file does not exist /path-to-ladder/tgf" where tgf is the ladder prefix