tags:

views:

85

answers:

1

Hi, I see sites that use this convention.

hxxp://www.twitter.com/golem

hxxp://www.twitter.com/golem/following

I am trying to do that on my site. "golem" is dynamic and is basically the username.

So hxxp://www.mysite.com/golem would go to the golem page.

And hxxp://www.mysite.com/golem/details.php would go to the details page for golem.

The question is:

  1. How do I make htaccess so it always adds &user_name=golem to all my php files in the query string? Without showing it in the browser address bar. So basically it needs to take the first path of the URL that has slash and add it as query to end of whatever PHP page I am looking at.

  2. How to make it so hxxp://www.mysite.com/golem/details.php actually runs file in real location at hxxp://www.mysite.com/details.php?user_name=golem (no "golem" in path) WHILE still display the fake URL in address bar.

  3. Make it work for other urls like hxxp://www.mysite.com/golem/page.php and hxxp://www.mysite.com/golem/view.php and etc etc

Looked at tutorials etc but cannot find answer. Appreciate any help!

+1  A: 

Enable mod_rewrite if not already done. In your .htaccess, you must add some RewriteRule.

Try this:

RewriteRule ^([^/]+)/([^?]*)\??(.*)$ $2?$3&user_name=$1 [L]

If it doesn't work, try with this one:

RewriteRule ^/([^/]+)/([^?]*)\??(.*)$ $2?$3&user_name=$1 [L]

But there is a counterpart: if you have classic URLs, they will be caught by these rules. If you can distinguish usernames from script names, there is a solution. Let's say the usernames can't contain a dot. Then, you would replace the rule with this one:

RewriteRule ^([^/.]+)/([^?]*)\??(.*)$ $2?$3&user_name=$1 [L]

(Notice the dot in the first parenthesis -- it says that if the url contains a dot, then it won't match the rule).

Now every url containing a dot before the parameters will be treated as a "normal" url.

If the usernames CAN contain a dot, we're facing a problem, because how can you say that details.php is the name of a php script or a username ? In these cases, it becomes very difficult to write RewriteRules that match only what we want. And it's also very difficult to test them, because there are a lot of edge cases it's very difficult to think of.

FWH
Thanks so much FWH. It seems to work but for some WEIRD reason, I can no longer read other parts of the URL parameters. I can get the username using $_REQUEST['user_name'] but if the URL is like hxxp://www.mydomain.com/details.php?item_id=23 then I cannot get item_id off the URL for some weird reason. Anyone know why?
This is because the above rules catch every url and treat the first part as the username, even when it is "details.php". If you can distinct the usernames from the script names (let's say, for example, if usernames can't contain a dot), then you should replace the rules with something which will only catch urls without script names. I'm updating my answer to reflect this.
FWH
Thanks FWH. But it still does not seem to work. I did a request for the item_id in URL but it wont give it to me. It does give me username however.
I guess what I am saying is that I would like the following: hxxp://www.mydomains.com/golem/details.php?item_id=22. In my script, I would like to get both the item_id of 22 and username of golem. Is that possible? Are you saying if URL has .php, it wont be able to get BOTH username off the "golem" and item_id of 22?