Page 1 of 1

mod rewrites and $_GET[] strings [SOLVED]

Posted: Tue Feb 27, 2007 6:43 am
by s.dot
I have a simple mod rewrite that rewrites domain.com/username to domain.com/user-view.php?user=username.

The problem comes when I want to append information to that url.. ie

domain.com/username?success=1&album_created=12345

Say I wanted to get the value of $_GET['success'] or $_GET['album_created'] ..

They don't appear in the $_GET array.

Do I have to make a rewrite rule like this?

Code: Select all

RewriteRule ^([\w]{3,25})(.+)$ /user-view.php?user=$1&query_string=$2
Then use php to break apart the $_GET['query_string']?

Is there any easier way?

[edit] I don't want to use user-view.php?user=username&success=1&album_created=12345. I want it in the form of domain.com/username?query=string

Posted: Tue Feb 27, 2007 7:17 am
by fastfingertips
There are two ways to make this:

- First you may use the Apache mod-rewrite, if so indeed you will have to add such line. This can be saved in a htaccess file if you are allowed.
- Otherwise as i remember you can make this in php with redirect to 404 or something like that.

Posted: Tue Feb 27, 2007 8:18 am
by s.dot
well, to make things a little bit clearer

i would like to be able to use the following

domain.com/username
domain.com/username?some=query&string=foo

With using the mod rewrite to allow the first one, the only thing that gets passed via $_GET is $_GET['user']. How do I make a query string appended to that (such as the second example) available to PHP?

Posted: Tue Feb 27, 2007 8:21 am
by Nathaniel
Untested, but try:

Code: Select all

RewriteRule ^([\w]{3,25}) /user-view.php?user=$1&%{QUERY_STRING}
Note the missing $ after the regex.

Posted: Tue Feb 27, 2007 8:49 am
by s.dot
Thank you.

With that, the values are passed via $_GET

Code: Select all

Array
(
    [user] => scrotaye
    [success] => sub-album-created
)
However, regular pages like index.php and faq.php trigger the rewrite rule (resulting in "sorry, user index.php does not exist" as it's passing "index.php" to view-user.php?user=index.php)

The exact rewrite rule is..

Code: Select all

RewriteRule ^([\w]{3,25})(/)? view-user.php?user=$1&%{QUERY_STRING}
[edit] HOWEVER, if I add the $ to the end of the regex, it works perfectly. Kinda ironic that's what you pointed out not to put in :lol:

But thanks man!