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 work开发者_JS百科s 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)
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.
You are talking about URL rewriting. You need .htaccess and mod_rewrite.
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'];
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.
精彩评论