Reputation: 21
My issue is that I would like my users to have a custom url http://mywebsite.com/username
.
I have read about the .htaccess file
, but I am with GoDaddy so I believe I need to do something with the web.config file
.
The issue is that currently my user profile URL doesn't display and PHP code that is specific to any user. It just says http://mywebsite.com/profile for every user (site still in test mode so none of the users are real right now).
How do I make the url rewrite work when the profile url currently isn't displaying any specific user info like user?id=3
or whatever? And how do I at least get the current profile to grab specific user info?
Upvotes: 0
Views: 2981
Reputation: 892
I personally would have the application perform all of the necessary routing logic instead of relying on .htaccess. This is how I would do it:
I would start by using a pre-built router class/module/library. If you are coding in PHP, use something like Alloy Router. If you are using a framework of some sort, routing might already be built in.
Next step would be to make a "catch all" profile route that catches all routes that haven't previously been "caught". You might have a bunch of other routes such as "website.com/about", "website.com/contact", etc, which will get caught first and then the catch-all route will get all others.
The catch-all route will route your application to a "profile" controller. This controller looks at the given route and checks to see if the "username" within the route matches a username in your database. If so, create the profile of that particular user. If not, provide a 404.
You'll have to make sure all usernames are unique and they don't conflict with other routes (i.e. a user can't have the username "about" otherwise their url will be caught by the "about" route).
Finally, within your application you will have to make all of your links go to the proper urls. If you have a "profile" button in a top navigation bar, you will want to make sure it actually goes to "website.com/username" where username is the actual username of the currently logged in user. All links to other user's profiles will be dynamically created to go to the correct url that includes their usernames.
Upvotes: 1