mod_rewrite and $_GET (PHP)

Associate
Joined
14 Oct 2008
Posts
416
I've got some stuff I need to do with mod_rewrite but I'm totally new to it and can't get my head around it.

I've used an example online to get the basics working... so I'm at a stage now where http://example.com/mypage/ brings up mypage.php.

Now I wanted to be able to tag stuff onto the end of that url which gets put into the $_GET array. For example: http://example.com/mypage/id/1/ would create an entry in $_GET with id=>1.

Is this possible?

Hope all that makes sense. As I said, I'm pretty new to both PHP and mod_rewrite.

Thanks in advance.
 
RewriteRule ^/([a-z]+)/([a-z]+)$ /parameter.php?$1=$2

http://webdesign.about.com/od/mod_rewrite/a/aa071006.htm

Doesn't seem to work :( I just keep getting a 404... I'm pretty sure it's a problem with the MVC tutorial I followed to set up all my other stuff though because I'm getting the custom 404 message from that.

Basically, I've got "controller" pages like login.php and inside them, each function controls what page loads. For example if I had these functions inside login.php:

index()

test()

anothertest()

If the user went to /login it would run the index() function.
test() would go to /login/test.
anothertest() would go to login/anothertest etc.

I think that's making things more complicated than they usually are with mod_rewrite.

May have to just scrap the pretty urls idea and go with /login?id=2 or whatever unless anyone fancies giving me a hand via email/msn or something? Can't really post all the code on here unfortunately.

(It's not actually a login page I'm trying to do btw... just did that as an example to save me explaining everything).

Edit - Oops, suppose I should put my current rewrite rule in here incase anyone has any ideas:

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

I also tried adding:
RewriteRule ^/([a-z]+)/([a-z]+)$ /mytrip.php?$1=$2

above and below the current rule after Pho posted to fix the problem and still had no joy :(
 
Last edited:
I really (really) need to learn regex properly because I spend ages getting this stuff to work. Try this:

.htaccess:
PHP:
Options +FollowSymLinks

RewriteEngine On

# Ignore certain filetypes
# (http://www.workingwith.me.uk/blog/software/open_source/apache/mod_rewriting_an_entire_site)
RewriteRule \.(css|jpe?g|gif|png|js)$ - [L]

# Redirect to index.php
# http://www.webmasterworld.com/apache/3370007.htm
RewriteRule ^(([^/]+/)*([^./]+))/?$ index.php?q=$1 [L]

index.php:
PHP:
<?php
# Exploding from:
# http://www.webdesign.org/web/web-programming/php/rewritten-urls-with-unlimited-parameters.10144.html
setInput('q');
echo "<pre>"; print_r($_GET); echo "</pre>";

function setInput( $var = '' )
{
      $input = explode( '/', $_GET[$var] );

      for( $i = 0; $i < count( $input ); $i++ )
      {
            if( $i != 0 )
            {
                  $input_array[ $input[ $i ] ] = $input[ $i + 1 ];
                  $i++;
            }
      }

      $input_array['act'] = $input[0];

      unset( $_GET );
      $_GET = $input_array;   
} 
?>

Using this a URL such as:
http://example.com/login/user/pho/action/destroy

Is sent to index.php and the $_GET variables are re-wrote to:
Array
(
[user] => pho
[action] => destroy
[act] => login
)

You can use your controller to do whatever you like with act (i.e. include login.php).
 
Sorry I took so long to reply to this. Didn't have chance to test it for a while.

I didn't use this exact solution but managed to get it working with what you posted. Thanks! :)
 
:) np. It's all good practice for me anyway :D.

This way should allow you to have as many variables passed as you like (to the limit of URL lengths etc I guess) rather than just passing a couple of variables like those written purely in .htaccess do.
 
Back
Top Bottom