1

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!

Community
  • 1
  • 1
  • The rewriting happens in the server, the user will see the original URL they entered and the web server will rewrite the url for your scripts. – JimL Feb 05 '17 at 11:50
  • 2
    The rewrite doesn't mean that a user will be redirected to another URL, but i means that the server will internally translate "/john" into "?user=john". – Jan Rydrych Feb 05 '17 at 11:53
  • alternatively you could spoof the url with javascript's window.history.pushState once the page is loading. See http://stackoverflow.com/questions/24307401/window-history-pushstate-refreshing-the-browser – Louis Loudog Trottier Feb 05 '17 at 11:54
  • on apache check mod_rewrite – Robert Feb 05 '17 at 11:55
  • Possible duplicate of [Simple Mod Rewrite](http://stackoverflow.com/questions/7093190/simple-mod-rewrite) – Robert Feb 05 '17 at 11:56
  • Avoid pushstate if possible, doesn't work in all browsers. – Juned Feb 05 '17 at 11:57
  • Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Croises Feb 05 '17 at 12:21

3 Answers3

1

Usually who needs this kind of feature uses Routers.

Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.

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:

$router->get(['/user/{name}', 'username'], function($name){
    //retrieve $name information
    return 'Hello ' . $name;
})

Just for information, every MVC framework uses router as standard.

Veenz
  • 64
  • 1
  • 4
0

Using .htaccess File

RewriteEngine on
RewriteRule ^/([a-zA-Z0-9])$ /users.php?name=$1

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 but users.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.

rummykhan
  • 2,343
  • 16
  • 33
0

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 R|redirect flag to a rule, e.g.

RewriteRule ^foo$ /bar.html [R,L]

or when the target is an absolute URL containing a domain, e.g.

RewriteRule ^foo$ http://www.example.com/bar.html [L]

Rewrite on the other side, means take the request and send an answer somehow, without redirecting the client to another resource.

RewriteRule ^foo$ /bar.html [L]

This time, the client asks for foo, but the server sends the contents of bar.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.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^john$ /users.php?name=john [L]

or more general

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /users.php?name=$1 [L]
Olaf Dietsche
  • 66,104
  • 6
  • 91
  • 177