I am attempting to rewrite some query strings I've during my URL such as this :

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

to

foo.com/admin/news/edit/3

But can't decipher it...

it will also works basically have only the 'page' parameters for instance, since i have don't also have these 4 parameters (page,view,action,id) during my web addresses, obviously.

any suggestions?

The simplest strategy is to complete the majority of the parsing in PHP code.

Use rewriterules like

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

After which access the request uri ($_SERVER['REQUEST_URI']) from PHP, and parse it into as numerous parts as you would like.

I authored this code to parse the type of web addresses you need to use into a range of parameters. It's flexible, meaning you should use the parameters in completely different ways on every page. You'll need it utilize it having a request handler like Joel L recommended.

Additionally, it enables for old-school $_GET query strings to become understood just just in case, and in addition it overwrites $_SERVER['PHP_SELF'] some thing as if you expect it to, with the road to the controlling script as opposed to the entire url with your parameters.

I rapidly edited it to be used like a standalone function, so be careful for typos. Also, any tips 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 make use of your site's domain.

If you make use of this around the request of: http://site.com/news/category/arts/page/5

You're going to get the next variable back:


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

Inside your .htaccess file put both of these lines:

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

Next, rather than calling your website like "example.com/index.php?var=1&var=-..Inch give them a call such as this "example.com/1/-..Inch

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

Requires only mod_rewrite, all parameters are optional.