Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nginx rewrite to hide or clean URLs?

Greetings.

I've been trying to grok regexes and rewrites but i'm a newbie at it. I have a simple site, one dir only, all i want is to rewrite domain.com/file.php?var=value to domain.com, making sure the user will only see domain.com in the adress bar throughout site navigation (even if i start making the site more complex).

Simply speaking it's freezing the url, although if the site grow's i'd rather make some "clean url" approach but i'm still at basic php and i sense i'd have to rtfm on http/1.1


2 Answers

You can use nginx rewrite rule like this:

rewrite  ^([^.]*)$  /index.php

to send all requests that do not contain a "." to your index.php script. In your php script, explode the request url to get the passed parameters, and then call the php page you want to:

#URL /people/jack_wilson
$url_vars = explode('/',$_SERVER['request_uri']); # array('','people','jack_wilson');
$page = $url_vars[1];
if($page == 'people'){
   $username = $url_vars[2];
   require('people.php'); #handle things from here.
}
like image 127
Daniel Von Fange Avatar answered Nov 18 '25 20:11

Daniel Von Fange


Typically this type of "clean" URL stuff is typically accomplished with a wrapper frame for the entire site. The browser shows the URL of that frame only, and the contents can be whatever.

But I actually don't recommend it, because the user may want to bookmark the "dirty" URL.

This type of URL obfuscation diminishes usability for advanced users.

Not to be a jerk, but I typically only see this type of URL obfuscation on "artsy" sites that care more about what their address bar looks like than usability of their site. Making your users happy via enhanced usability is a better long term approach IMHO.

like image 34
Trampas Kirk Avatar answered Nov 18 '25 19:11

Trampas Kirk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!