htaccess url rewrite/redirect - like facebook does
.htaccess
Solution
URL rewriting step by step...
Step 1 - decide how you want client-side URLs to look
You've done this part - you want `example.com/peter` to be what your user sees
Step 2 - work out the server-side URLs
Technically, you could produce a huge list of hard coded rewrite rules, e.g.
RewriteRule ^peter$ mypage.php?id=324
RewriteRule ^alice$ mypage.php?id=325
RewriteRule ^bob$ mypage.php?id=326
But that's not particularly efficient when you have a lot of users.
Your mypage.php will accept an id, but you'll really need to modify it so that it can accept a possible username (but assume it will contain all sorts of garbage). Let's say you decide to allow it to take another query string argument 'hint'. So now you have to get a URL like this working: `example.com/mypage.php?hint=peter`
Ensure your script produces a 404 response for any username it doesn't understand.
Step 3 - product rewrite rules to map client-side to server-side URLS
One way to do this would be something like this - the first RewriteCond line ensures the rule only fires if the request doesn't match an actual file. The RewriteRule will cause the request to be handled by mypage.php, but passing in the rest of the URL as the 'hint' paramter. The 'qsa' part is short for 'query string append', which means any query string present in the original URL is added to the written rule. The 'l' means 'last', so that no further rules are invoked to handle this request.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)$ mypage.php?hint=$1 [qsa, l]
In mypage.php you'd then look up the user using the hint.
Note I've been pretty restrictive in the pattern, only sequences of lower case letters allowed. This will help ensure your script isn't invoked for every 404 your site might produce .
Problem
I have a url like `www.example.com/mypage.php?id=324` That number 324 is auto incremented (meaning that person is 324th user of the website), and each user can set own user url, which is stored in mysql table (alphanumeric only). Let's say, user number 324 has user url 'peter' I am trying to replace `mypage.php?id=324` to peter, so the url can be displayed like `www.example.com/peter` But I still want to maintain `mypage.php?id=324` internally, so if someone types `www.example.com/mypage.php?id=324`, it can be redirected to `www.example.com/peter` I guess this is vanity url like facebook does? I've searched all over the web, and even bought a book about htaccess, but I still couldn't figure this out. Please help me! Thank you so much in advance!