Reputation: 141
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!
Upvotes: 3
Views: 2783
Reputation: 300865
URL rewriting step by step...
You've done this part - you want example.com/peter
to be what your user sees
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.
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 .
Upvotes: 7
Reputation: 2084
But I still want to maintain mypage.php?id=324 internally, so if someone types www.example.com/mypage.php?id=324, redirected to www.example.com/peter
The url you suggested is not recommended, have something like http://www.example.com/users/peter
is better, then you can have rewrite rule like..
RewriteRule ^/users/(.*) /mypage.php?username=$1 [QSA,L]
And in you mypage.php, you can try
<?php
if (isset($_GET["id"])) {
// Get the $username from DB
// Do the redirection to http://www.example.com/users/$username if found,
// else send 404
} else if (isset($_GET["username"]) {
// normal flow with $username
}
Upvotes: 3