0

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!

hakre
  • 193,403
  • 52
  • 435
  • 836
Pankaj
  • 954
  • 8
  • 15

2 Answers2

1

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>

source: http://neziric.org/2010/06/dynamic-apache-vhosts-2/

metaforce
  • 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
1

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.

Kris
  • 2,108
  • 18
  • 19