Abishek
Abishek

Reputation: 11691

subdomain rewriteurl in PHP

I have set up my domain to point all wildcard subdomains to my webserver. What I would like to do is to perform a rewrite url based on the value that I receive on the wildcardsubdomain.

This is a specific scenario which I am not sure how to over come.

username.mydomain.com to rewrite to mydomain.com/user.php?userid=username

&

groupname.mydomain.com to rewrite to mydomain.com/group.php?groupid=groupname

I am storing on my db on a table the types as below.

john->userid
technology->groupid
steve->userid
Macleen->userid
Sports->groupid

Would this be helpful to acheive this programatically? How can I acheive this using rewriteURL?

And I would also like to keep the URL's of the page as is till the user navigates to another page from either user.php or group.php

Upvotes: 2

Views: 236

Answers (2)

voondo
voondo

Reputation: 2583

index.php :

<?php
if(preg_match('/([a-z]+).mydomain.com/i', $_SERVER['SERVER_NAME'], $matches)){
  $subdomain = $matches[1];
  if(YourModel::isGroupName($subdomain)){
    $_GET['groupid'] = $subdomain;
    require './group.php';
  } else {
    $_GET['userid'] = $subdomain;
    require './user.php';
  }
}

Upvotes: 1

Dan
Dan

Reputation: 12096

Try the following:

RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.com$ [NC]
RewriteRule ^$ file.php?id=%1 [L]

You could then place some sort of id check in file.php to find out if it's a username or groupname, would only work if you had unique ids for both combined though as there's no way to seperate random.mydomain.com from random.mydomain.com.

You could then use javscript to format the url:

 history.pushState({path: "url"}, "", "http://random.mydomain.com/user");

Upvotes: 1

Related Questions