sazzle Posted June 23, 2011 Share Posted June 23, 2011 Hello, I'm new to this forum and reasonably new to PHP as well! Please forgive me if my question is a simple one. Briefly, here is what I am trying to achieve: a website where a user signs up, logs in, completes a profile, and can choose whether or not they want to make their profile public (visible to anyone who has the correct URL). I would like a user's public profile to have a URL of this format: domain.com/username. Then, that page should recognise which user's profile the visitor is looking for, and, if that user's profile is public, display the profile or, if the user's profile is not public, display an error message. I don't know how to get from domain.com/username to being able to run a query using that username. Perhaps there will be some .htaccess stuff involved to allow visitors to type domain.com/username instead of domain.com/profile?username=username ?? I'm not sure. Any help or pointing in the right direction would be very much appreciated! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/240193-a-question-about-urls/ Share on other sites More sharing options...
Adam Posted June 23, 2011 Share Posted June 23, 2011 You need to use mod_rewrite to internally rewrite the URL. While the user sees "domain.com/username", it's actually pointing to "domain.com/profile.php?username=username". This is actually quite a common request, and the very basic rewrite rule would be: RewriteEngine on RewriteRule ^[a-z0-9]+$ profile.php?username=$1 [L] Be sure to modify the regex to allow whatever characters/format your usernames are made up of. Quote Link to comment https://forums.phpfreaks.com/topic/240193-a-question-about-urls/#findComment-1233769 Share on other sites More sharing options...
sazzle Posted June 23, 2011 Author Share Posted June 23, 2011 Thank you for your quick reply, that sounds perfect! Quote Link to comment https://forums.phpfreaks.com/topic/240193-a-question-about-urls/#findComment-1233778 Share on other sites More sharing options...
sazzle Posted June 24, 2011 Author Share Posted June 24, 2011 Thank you for that code - I've now tried it out and it works perfectly. I have one other question. I need to get the username from the URL (which is now displayed as domain.com/username instead of domain.com/profile.php?username=username) to use in a mysql query to display the correct information in the profile. I tried setting $username = $_GET['username'] but that doesn't work when viewing the page as domain.com/username. Is there another way in which I can achieve this? Thank you and again I apologise if my question is a simple one. Quote Link to comment https://forums.phpfreaks.com/topic/240193-a-question-about-urls/#findComment-1234253 Share on other sites More sharing options...
Adam Posted June 24, 2011 Share Posted June 24, 2011 It should work. Can you post a print_r of $_GET? Quote Link to comment https://forums.phpfreaks.com/topic/240193-a-question-about-urls/#findComment-1234255 Share on other sites More sharing options...
sazzle Posted June 24, 2011 Author Share Posted June 24, 2011 When I view the profile page as domain.com/profile.php?username=sarah and do <?php print_r($_GET); ?> the following is displayed: Array ( [username] => sarah ) When I view the profile page as domain.com/sarah and do <?php print_r($_GET); ?> the following is displayed: Array ( [username] => ) Thank you for helping me! Quote Link to comment https://forums.phpfreaks.com/topic/240193-a-question-about-urls/#findComment-1234258 Share on other sites More sharing options...
Adam Posted June 24, 2011 Share Posted June 24, 2011 Ah, course: RewriteEngine on RewriteRule ^([a-z0-9]+)$ profile.php?username=$1 [L] Forgot the parentheses that create the back reference to the username. Quote Link to comment https://forums.phpfreaks.com/topic/240193-a-question-about-urls/#findComment-1234260 Share on other sites More sharing options...
sazzle Posted June 24, 2011 Author Share Posted June 24, 2011 Thank you so much, that's perfect Is it also possible to make it so that if someone views /profile.php?username=sarah it would redirect to /sarah? Not too important, but that would make it a little nicer :-) Quote Link to comment https://forums.phpfreaks.com/topic/240193-a-question-about-urls/#findComment-1234262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.