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!
-
3This is not a task for PHP alone. What server environment are you on? – Pekka May 18 '11 at 11:12
-
1possible duplicate of http://stackoverflow.com/questions/79764/wildcard-subdomains – jimy May 18 '11 at 11:13
-
1stack.domain.com will be at dns side you need to setup subdomain yeah domain.com/stack this could be possible with php – Vishwanath Dalvi May 18 '11 at 11:15
2 Answers
you should check if your webserver has those "wildcards", then you could achive this by writting a magic virutal host witch will handle that.
try a virtual host like this:
<VirtualHost *:80>
ServerName local
ServerAlias *.local
VirtualDocumentRoot /var/www/%1/public_html
UseCanonicalName Off
</VirtualHost>

- 1,337
- 5
- 17
- 26
-
I have bought the server space. So this virtual host configuration of apache cant help. one and only way is to do it by my site itself. – Pankaj May 18 '11 at 12:02
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 verifying something
is 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 /something
that is already a valid page is not a valid username. Alternatively you could use a prefix like domain.com/u/user
to be more flexible in the names of your own pages.

- 2,108
- 18
- 19