dusker
dusker

Reputation: 580

Directory style redirects in PHP

it's been a very very long while since i did anything in PHP. Would anyone care to explain how to achieve such effect:

i go to mydomain.com/user/peter and get a customized user page for this username 'peter' can it work such that peter gets piped to some php script that generates page for user parameter? it kind of works like passing parameter via url but looks nicer, how is this thing called?

(i know that this sounds weird but i'm sure you advanced php coders will know what i'm asking about)

Upvotes: 0

Views: 91

Answers (4)

Dutchie432
Dutchie432

Reputation: 29160

I know this was answered already, but IIS users (like myself) can use URL Rewrite 2.0 to accomplish the same task with Regular Expressions. I've installed it and it works well.

Upvotes: 0

bradenkeith
bradenkeith

Reputation: 2423

You're looking for .htaccess mod_rewrites.

http://devmoose.com/coding/20-htaccess-hacks-every-web-developer-should-know-about

Basically you want to write a rule that sends all URLS of user/peter to user.php?username=peter. From there you can manipulate with php echo $_GET['username'];

Upvotes: 0

Artefacto
Artefacto

Reputation: 97835

If using Apache, mod_rewrite can solve this:

RewriteEngine on
RewriteRule /user/([^/]+) /user.php?name=$1 [B,QSA]

If in an .htaccess file in the root:

RewriteEngine on
RewriteRule user/([^/]+) user.php?name=$1 [B,QSA]

See the docs for mod_rewrite.

You can also enable MultiViews and then those requests will be mapped to user.php automatically. You can access the value after user with $_SERVER['PATH_INFO]`:

Options +MultiViews

See the docs for content negotiation.

Upvotes: 4

code_burgar
code_burgar

Reputation: 12323

You are talking about URL rewriting. You need .htaccess and mod_rewrite.

Upvotes: 1

Related Questions