My website functions a little like 9gag or damnlol, etc. when the user visits an item page, for example: site.com/104-funny-thing, the rewrite rule redirects to view.php?id=104-funny-thing. Here the URL is stripped away of everything after the first '-'. So we are left with the id only, in this case: 104, and the query gets the item.
Here is the Rule:
RewriteRule ^([A-Za-z0-9-]+)$ view.php?id=$1
What I want is that the same URL format could be used to open up user profiles as well. For example: site.com/salman, this URL should open up profile.php?username=salman.
I thought of doing this via PHP. Where I can use the same file, view.php, for both purposes. Once the parsing of the URL is done, if I get a numeric value, I would include the item.php page and query and show data accordingly. If it's not numerical, then I ca include the profile.php page and show the profile instead. This should theoretically work flawlessly, but what I wanted to try was that if there was a way to redirect using the .htaccess directly, I would prefer to use that option, as the previous option requires extra work to sort out some issues that the solution causes.
Someone good at htaccess (regex code?) might be able to offer a solution. Here is some further explanation:
- Usernames can contain numbers, but not only numbers
- Username URLs cannot contain '-'
- Item IDs cannot contain alphabets
- URLs 104-funny, 104- and 104, all work for view.php
Thank you for your help in advanced!