php - Have user query www.example.com/parameter and use that parameter without changing URL in url bar
So basically I have a website with user profiles. I want users to be able to query a profile like this: www.example.com/john
and that would trigger another page I've written in PHP to display that profile but WITHOUT then changing the url. I've looked at other examples on SO like this: How do I achieve a url like www.example.com/username to redirect to www.example.com/user/profile.php?uid=10?
However this uses url rewriting which means the URL would then change. For instnace if the page is called users.php, I don't want this to happen: user queries www.examples.com/john -> page is changed to www.examples.com/users.php?name=John
I want the url to stay as www.examples.com/john but for the server to serve up the page www.examples.com/users.php?name=John
Would I still use url rewriting to achieve this even though I don't want the url to change in the url bar? Hope someone can help, thank you!
Answer
Solution:
Usually who needs this kind of feature uses Routers.
Basically you can take your url and divide it in parameters. The response it's related to the input url. There are some good libraries in php which allows you to handle routers, for example:
In phroute you can solve your problem just with:
Just for information, every MVC framework uses router as standard.
Answer
Solution:
Using .htaccess File
I hope this will solve your issue, now what it will do, on the front or to the public url is like
http://www.example.com/john
butusers.php
script on your server will receive a GET parameter name which will have the value john.Just make sure apache mod_rewrite is turned on.
Answer
Solution:
With Apache's rewrite module, you can do a rewrite or a redirect.
Redirect means, send a response (status code 301, 302) to the client and ask to fetch another URL. This causes the browser's URL bar to change to the new URL. This happens, when you add the
flag to a rule, e.g.
or when the target is an absolute URL containing a domain, e.g.
Rewrite on the other side, means take the request and send an answer somehow, without redirecting the client to another resource.
This time, the client asks for
foo
, but the server sends the contents ofbar.html
without telling the client.So, as long as you don't add the
R
flag, or give an absolute URL, the client's URL bar won't change, e.g.or more general