allaire
allaire

Reputation: 6045

mod rewrite and query strings

I'm trying to rewrite some query strings I have in my URL like this :

foo.com/index.php?page=admin&view=news&action=edit&id=3

to

foo.com/admin/news/edit/3

But can't figure it out...

it should also works if I only have the 'page' parameters for example, since I don't always have these 4 parameters (page,view,action,id) in my urls, of course.

any suggestions?

Upvotes: 2

Views: 713

Answers (4)

h0tw1r3
h0tw1r3

Reputation: 6818

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*) index.php?page=$1&view=$2&action=$3&id=$4

Requires nothing but mod_rewrite, all parameters are optional.

Upvotes: 0

Syntax Error
Syntax Error

Reputation: 4527

I wrote this code to parse the kind of urls you want to use into an array of parameters. It's flexible, meaning you can use the parameters in totally different ways on every page. You will need it use it with a request handler like Joel L suggested.

It also allows for old-school $_GET query strings to be understood just in case, and it also overwrites $_SERVER['PHP_SELF'] to behave like you expect it to, with the path to the controlling script rather than the entire url with all your parameters.

I quickly edited it for use as a stand alone function, so watch out for typos. Also, any suggestions to improve this code are welcome.

<?php 
function parseRequestUrl($http_server = 'http://example.com'){

    $request = $_SERVER['REQUEST_URI'];
    $request = explode('?', $request);
    if (isset($request[1])){
        $get_data = $request[1];
        parse_str($get_data, $_GET);
    }
    $request = $request[0];

    $url_data = array_values(array_filter(explode('/', $request)));
    $url_level = substr_count($http_server, '/', 8);
    foreach($url_data as $key => $val){
        if($key>$url_level){
            $parameter[] = $val;
        } else if ($key==$url_level){ // controller is also stored at $this->parameter[0]
            $parameter[] = $controller = $val;
        }
    }

    if(!isset($controller) || !$controller){
        $parameter[0] = $controller = 'index';
    }
    $_SERVER['PHP_SELF'] = $http_server.'/'.$controller;

    return $parameter;
}
  ?>

For $http_server use your site's domain.

So if you use this on the request of: http://site.com/news/category/arts/page/5

You will get the following variable back:


$parameter[0] => 'news'
$parameter[1] => 'category'
$parameter[2] => 'arts'
$parameter[3] => 'page'
$parameter[4] => '5'

Upvotes: 3

Joel
Joel

Reputation: 3060

The easiest way is to do most of the parsing in PHP code.

Use rewriterules like

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule . index.php [L]

And then access the request uri ($_SERVER['REQUEST_URI']) from PHP, and parse it into as many parts as you want.

Upvotes: 4

Ben
Ben

Reputation: 16533

In your .htaccess file put these two lines:

RewriteRule ^/([^/]+)$ index.php?page=$1 [L]
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?page=$1&view=$2&action=$3&id=$4 [L]

Next, instead of calling your pages like "example.com/index.php?var=1&var=2 ..." call them like this "example.com/1/2 ..."

Upvotes: 0

Related Questions