I am developing a site where users will have permalinks for them like user.domain.com or domain.com/user and i want to achieve this by php code. foe ex.
for username stack the permalink is stack and on going to stack.domain.com or domain.com/stack it should go to profile page of user!
I am developing a site where users will have permalinks for them like user.domain.com
Share
Let’s split the two cases:
user.domain.com
For this to work, you should first configure your DNS so that *.domain.com points to your server. Then in index.php, you can check if
$_SERVER["HTTP_HOST"]matches something.domain.com (using e.g. preg_match). After verifyingsomethingis a valid username, you can either display the user’s profile page or redirect to the profile.Caution: make sure that any subdomain you use yourself, like for example
www., is not a valid username.domain.com/user
To implement this, you need to setup some kind of catch-all for non-existing pages. One way would be to instruct your webserver to serve a php-file when it encounters a 404. This file could then use the
$_SERVER["REQUEST_URI"]variable to determin if there if a user profile is requested.Caution: Make sure that any
/somethingthat is already a valid page is not a valid username. Alternatively you could use a prefix likedomain.com/u/userto be more flexible in the names of your own pages.