开发者

How to achieve subdomain type domain names with php

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 goin开发者_如何学JAVAg to stack.domain.com or domain.com/stack it should go to profile page of user!


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.


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜