Search code examples
php.htaccessmod-rewrite

.htaccess rewrite GET variables


I have a index.php which handle all the routing index.php?page=controller (simplified) just to split up the logic with the view.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [NC]

Which basically: http://localhost/index.php?page=controller To

http://localhost/controller/

Can anyone help me add the Rewrite for

http://localhost/controller/param/value/param/value (And soforth)

That would be:

http://localhost/controller/?param=value&param=value

I can't get it to work with the Rewriterule.

A controller could look like this:

    <?php
if (isset($_GET['action'])) {
 if ($_GET['action'] == 'delete') {
do_Delete_stuff_here();
}
}
?>

And also:

    <?php
if (isset($_GET['action']) && isset($_GET['x'])) {
 if ($_GET['action'] == 'delete') {
do_Delete_stuff_here();
}
}
?>

Solution

  • Basically what people try to say is, you can make a rewrite rule like so:

    RewriteRule ^(.*)$ index.php?params=$1 [NC, QSA]
    

    This will make your actual php file like so:

    index.php?params=param/value/param/value
    

    And your actual URL would be like so:

    http://url.com/params/param/value/param/value
    

    And in your PHP file you could access your params by exploding this like so:

    <?php
    
    $params = explode( "/", $_GET['params'] );
    for($i = 0; $i < count($params); $i+=2) {
    
      echo $params[$i] ." has value: ". $params[$i+1] ."<br />";
    
    }
    
    ?>