1

I have a sitename.net and in a root directory i have .htaccess and index.php .htaccess is:

FallbackResource /index.php

so basically as it's empty site (no sub directories, no files etc...) I will get a fallback to index.php which contains this:

$path = ltrim($_SERVER['REQUEST_URI'], '/');    // Trim leading slash(es)
$elements = explode('/', $path);                // Split path on slashes
echo "Domain is: $domain <br>";
if(empty($elements[0])) {                       // No path elements means home
    echo "ShowHomepage()";
} 
else switch(array_shift($elements)) {             // Pop off first item and switch
    case 'tests':
      echo "First is test";
    case 'sample':
      echo "First is sample";
    default:
        header('HTTP/1.1 404 Not Found');
        Show404Error();
}

my problem is that when visitor enters: https://username.sitename.net/tests/test1 I get error as subdomain doesn't exist... When I go to "https://sitename.net/tests/test1" i get what I want but I need username as a variable as well.

Should I first do rewrite in .htaccess so it translate https://username.sitename.net/tests/test1 as https://sitename.net/username/tests/test1 and than redo index.php so it pickup a first array element as username or there is another option ?

Can somebody help me out ?

------------- EDIT ----------------

I've ended up pointing A record *.sitename.net to server IP I've changed .htaccess so it's:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.sitename\.net
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]

FallbackResource /index.php

...and still getting 404 error... still can't get: https://username.sitename.net/tests/test1 to act like https://sitename.net/username/tests/test1 so index.php will do it stuff...

Peter
  • 1,264
  • 5
  • 20
  • 41

2 Answers2

1

I think wildcard DNS records is what you're looking for.

https://en.wikipedia.org/wiki/Wildcard_DNS_record

All subdomains will then redirect to your DNS, which you can redirect to your server.

EDIT: Once the subdomains are up and running, you can use .htaccess rewrite subdomain to directory to fix the htaccess.

Rentabear
  • 300
  • 2
  • 11
  • 1
    No, I actually need to translate https://username.sitename.net/tests/test1 to https://sitename.net/username/tests/test1 so the index.php kicks in... – Peter Sep 30 '19 at 15:02
  • If you add your server DNS from *.sitename.net to sitename.net, that is exactly what will happen. That's what DNS does. Once the DNS redirects you to your main domain, you can use htaccess to rewrite the URL. But none of it will work unless the subdomain actually exists. Which it won't unless you add the wildcard. – Rentabear Sep 30 '19 at 15:03
  • 1
    I get it now... Will give it a try now :) – Peter Sep 30 '19 at 15:05
  • https://stackoverflow.com/questions/10642426/htaccess-rewrite-subdomain-to-directory This can help once you get the subdomains working. – Rentabear Sep 30 '19 at 15:07
  • I've added wildcard *.sitename.net. CNAME to point to sitename.net and updated htaccess to:RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*)\.sitename\.net RewriteRule ^(.*)$ %1/$1 [L,NC,QSA] FallbackResource /index.php and still getting error... – Peter Sep 30 '19 at 15:17
  • Has to be an A-record. and it has to be *.sitename.net – Rentabear Sep 30 '19 at 15:27
  • I've added a star but it's lost in comment... Now issue is sitename.net is addon domain for sitename.com and CPanel allows me only to add IP as A record... and doesn't allow to have duplicated Cname and A records, so I'll remove Cname and add A record and will update here... – Peter Sep 30 '19 at 15:34
  • I've tried everything, but still no avail. I will update a question with the changes I did. – Peter Sep 30 '19 at 16:44
  • Can you remove the htaccess completely and see if username.sitename.net still 404's? If it does, there's still something wrong with the DNS. Granted DNS can take some time to resolve. – Rentabear Oct 01 '19 at 09:11
0

This work with php redirect too:

$url = "https://gbr8295.sitename.net"; //$_SERVER['HTTP_HOST']

if (substr($_SERVER['HTTP_HOST'], 0, 16) != 'https://sitename') { // You need edit 16 to a correct number for your domain
   $username = substr($url, 8, ((strpos($url, '.') - 8)));
   $new_url = "https://sitename.net/".$username."/";

   echo $url."<br>"; // Output: https://gbr8295.sitename.net
   echo $new_url; // Output: https://sitename.net/gbr8295/

   header("Location: $new_url");
   exit;
} else {
   echo 'ok';
}
Gabor
  • 566
  • 5
  • 14
  • I get PHP part, I have a problem with htaccess as https://username.sitename.net/tests/test1 doesnt' exist... actually NO SUBDOMAIN exists and there is no files in folder.... so I just need to figure-out a way to translate username.sitename.net/tests/test1 to sitename.net/username/tests/test1 so index.php is loaded and than I know what to do.... – Peter Sep 30 '19 at 15:04