Page 1 of 1

mod_rewrite first time use [SOLVED]

Posted: Wed Feb 13, 2008 8:24 am
by aceconcepts
Hi,

I've never actually used mod_rewrite but am constantly being nudged towards it.

I have already looked at this site http://www.4webhelp.net/tutorials/misc/mod_rewrite.php for some general info - seems quite straightforward.

However, im not absolutely sure on a few things:

When a Rewriterule has been declared how do I get the data to display.

i.e.

if you currently use a method like: /index.php?mode=books&id=456 how do you "GET" $mode and $id from the url: /books/456/

Thanks.

Re: mod_rewrite first time use

Posted: Wed Feb 13, 2008 9:11 am
by markusn00b

Code: Select all

 
$_mode = $_GET['mode'];
$_id = $_GET['id'];
 
T'is that simple ;)

Re: mod_rewrite first time use

Posted: Wed Feb 13, 2008 9:27 am
by aceconcepts
So using mod_rewrite, ultimately you should be able do the following:

unfriendly url: /registration?client=smith&id=123 should now be able to be viewed as: /registration/smith/123

Is this right?

Re: mod_rewrite first time use

Posted: Wed Feb 13, 2008 10:27 am
by aceconcepts
Ok, I have done some more research.

This is what I have: (all files are in the root dir)

File: .htaccess

Code: Select all

 
RewriteEngine On
RewriteRule index/(.*) index.php?parameters=$1
 
File: croute.php

Code: Select all

 
<?
 
$params = array();
 
function Router(){
    global $params;
    $explode = explode("/", $_GET["parameters"]);
    for($i=0; $i < count($explode); $i++){
        $params[$explode[$i]] = $explode[$i+1];
        $i++;
    }
}
 
Router();
 
?>
 
File: index.php

Code: Select all

 
<?php include("croute.php")?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Let's View An Article!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
foreach($params as $x1 => $x2){
    echo $x1.": ".$x2."<br />";
}
?>
</body>
</html>
 
My problem is that when i enter something like: http://www.site.com/some-variable it tells me that the directory does not exist!