-1

I am trying to achieve automatic subdomain creation. I have read a lot of tutorials including:

I understood the concept and I implemented it with success in the past for user profiles, but this is a different case and I am stuck.

What I want to do, is basically something like pen.io as functionality. A user creates a page with a password and then, that page name converts into a subdomain.

I thought of doing a function that runs on the index page of the main website and that one used afterwards in HTACCESS to have something like index.php?subdomain=test and that one to redirect to test.domain.tld

EDIT:

Here is the current implementation that works when clicking on a link, but it doesn't work when accessing the url directly from the browser:

Code used in view.php:

<?php

    include('inc/config.php');
$url = filter_var($_GET['url'], FILTER_SANITIZE_STRING);

$conn = new mysqli($server, $username, $password, $database) or die ('Unable to execute query. '. mysqli_error($conn));

$query = "SELECT * FROM `pages` WHERE pageTitle = '$url'";  
$result = $conn->query($query);

if($row = mysqli_fetch_array($result))
{
    $title = $row['pageEditableTitle'];
    $content = $row['pageContent'];
echo '<h5 class="mt-5"><mark>'.$title.'</mark></h5>
<p class="lead display-7">'.$content.'</p>';

} else {
echo '<br /><div class="alert alert-info" role="alert">Subdomain does not exist.</div>';
}

$conn->close();


?>

Code used in htaccess:

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld
RewriteRule ^(.*)$ https://domain.tld/view.php?url=%1 [L,NC,QSA]

But this redirects www.domain.tld to domain.tld/view.php?url=www and not staying as www.domain.tld in the browser url

FBK
  • 52
  • 5
  • 20
  • _“I thought of doing […]”_ - and? What is stopping you? // Please go read [ask], and then edit your question accordingly. – misorude Sep 05 '19 at 08:17
  • I wanted to know if I am on the correct path. I am not expecting full php code to be given to me. If I am doing it with index.php?subdomain=test, that will redirect when i click a link, but will it work after when someone access the browser via test.domain.tld or someone needs to click that link ? – FBK Sep 05 '19 at 08:20
  • Your server doesn’t now anything about “links” and “clicks”, the only thing it knows about, is that it received a request for a particular URL. What you do internally to handle that request, is up to you. – misorude Sep 05 '19 at 08:25

1 Answers1

0

I presuppose that you setup a wildcard dns entry (access random.domain.tld to test it!). Then you have two options:

Correct your rewrite rules

Something like [aA-zZ] should be [a-zA-Z] and the RewriteRule should be only after the RewriteCond and not in front of it and two of them. And do you really want to force a - inside the subdomain with ([a-z0-9][-a-z0-9]+)? Maybe you should check this answer. Note: The www inside of your domain is a subdomain as well. So it would rewrite to sub.php?url=www

With the corrected rewriting random.domain.tld returns the content of random.domain.tld/sub.php?url=random. But at the moment your sub.php does not return content. Instead it returns a http redirect to the URL random.domain.tld. This means your sub.php produces an infinite loop on itself. Instead sub.php should only contain something like <?php echo $_SERVER['HTTP_HOST']; ?>.

Maybe you did not understand how URL rewriting works. Then read this answer for further explanation.

Update1
You corrected your code as follows:

RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.domain\.tld\.?(:80)?$ [NC]
RewriteRule ([a-zA-Z0-9]+) /view.php?url=$1

But it's still wrong. As I said you need to read and understand this answer. @JoachimIsaksson uses $1 and %1 in his 2nd example:

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

%1 is the subdomain catched through RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com. And $1 is the path catched through RewriteRule ^(.*)$. You missed to use %1.

But your code can not work as you forced an unempty alphanummeric string by RewriteRule ([a-zA-Z0-9]+). But a path could contain more than that. For example a slash or question mark. And of course it could be empty as well.

And why did you add (:80)?? Do you think someone will access your domain with a specific port?

And why the last optional dot in tld\.??

At last you need to bring the flags into question. You used the NC flag. It means your rule is case-insensitive. So why do you use [a-zA-Z0-9]? As your rule is already case-insensitive it can be [a-z0-9]. And why don't you used the L and QSA flag? They are important.

Update2
Try this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld
RewriteRule .* view.php?url=%1 [L,NC,QSA]

Use PHP only

$_SERVER['HTTP_HOST'] contains your full domain. This answer explains how to extract the subdomain name:

$subdomain = array_shift((explode('.', $_SERVER['HTTP_HOST'])));

Now you are able to use your general index.php to switch between your general page or the users subdomain content:

$domain_parts = explode('.', $_SERVER['HTTP_HOST']);
// access without any subdomain (TLDs like "co.uk" would "need == 4")
if (count(domain_parts) == 3) {
    $subdomain = "www";
}
else {
    $subdomain = array_shift($domain_parts);
}
if ($subdomain == 'www') {
    // general page
}
else {
    // users page
}
mgutt
  • 5,867
  • 2
  • 50
  • 77
  • I do not understand correctly the htaccess part. I have modified it to be like so: RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.domain\.tld\.?(:80)?$ [NC] RewriteRule ([a-zA-Z0-9]+) /view.php?url=$1 ... I have created the wildcard subdomain in my hosting panel, and I am able to access random.domain.tld. Is this correct ? – FBK Sep 05 '19 at 09:52
  • @FBK You should post updates in your question. I updated my answer. – mgutt Sep 05 '19 at 12:46
  • Thank you for the detailed answer, I am trying to learn and put into practice what you just wrote to me. I updated the original post with latest code. It seems that when I access view.php?url=random, i get the response. When I am trying random.domain.tld -> I get the default webhosting page. When trying going to www.domain.tld, I get a internal server error... so I assume I am doing something wrong, will check and see where Webuzo stores it's logs, as that is what I have for control panel on my hosting. – FBK Sep 05 '19 at 13:20
  • @FBK You found a little mistake in the code of @JoachimIsaksson. If you want to use rewriting the target should not contain "http://" or "https://". This means `RewriteRule ^(.*)$ https://domain.tld/view.php?url=%1 [L,NC,QSA]` has to be `RewriteRule ^(.*)$ /view.php?url=%1 [L,NC,QSA]` – mgutt Sep 05 '19 at 14:35
  • Still not working for me, could it be because I have: Server Version: Apache/2.2.34 (Unix)? I am using CENTOS 7. I did the virtual host stuff, I created the * A record that points to my hosting IP... when i access any.domain.tld it shows the default Webuzo page, it is not redirecting to my view.php?url=any – FBK Sep 06 '19 at 08:36
  • Are you able to open any.domain.tld/view.php?url=any without the .htaccess? If not, your subdomain targets the wrong www folder. And what means default "webuzo page". You mean its loading the index.html in the root? Did you overwrite to verify your domain and wildcard dns targest the same folder? – mgutt Sep 06 '19 at 08:38
  • Right now my A record points to the IP of my hosting, should it point to my website folder ? I am not able to open any.domain.tld/view.php?url=any -> if I go there it says url not found. – FBK Sep 06 '19 at 09:02
  • I dug a little more and now if I go to any.domain.tld shows the content from domain.tld. .... if i go to any.domain.tld/view.php?url=any -> it shows the page – FBK Sep 06 '19 at 09:10
  • @FBK Ok, if you are now able to load the page, then you can go forward and use your .htaccess and open only any.domain.tld. If the .htaccess code is working it should display the content of domain.tld/view.php?url=any without redirecting on it. But beware of your browser cache. It caches redirects as well. The best way is to open every time a different url like any.domain.tld/?1 and on the next try any.domain.tld/?2 and so on. Or you disable caching in your browsers developer tools. – mgutt Sep 06 '19 at 09:13
  • No change. If I go to any.domain.tld i get the domain.tld page... If I go to any.domain.tld/whatever it shows the page, even if I do not write the full path like any.domain.tld/view.php?url=any .... I can go to any.domain.tld/x and it still shows what is under any. – FBK Sep 06 '19 at 09:53
  • @FBK Did your htaccess contain `RewriteEngine on`? Check my the 2nd update in my answer. I tested this code in my own hosting setup and it works. – mgutt Sep 07 '19 at 12:18
  • Everything works fine, I had some extra stuff in my htaccess that was making it not work. I placed your exact code and everything runs. Thank you so so much for all your help. – FBK Sep 08 '19 at 08:49