Mapping a custom domain for redirections (url shortener)

Hello everyone, thanks for reading and helping This is a recap of the situation: - I want to create a url shortener platform (for my projects) - Each tenant/project is accesible from the "{originaldomain}/admin/" (shared for all the projects, limited access depending user) - Each tenant/project has a profile field to add a custom domain - Each link I create should have this url structure: {custom_domain}/{string of characters} Shortened url creation process example: - Add {custom_domain} to the Tenant/Project (https:// customdomain .com) - Add final/original url (https:// google .com) - Add the shortened slug (abcd1ef) - Generated url » https:// customdomain .com/abcd1ef - If visit the Generated url = redirected to final/original url (https:// google .com) The problem is that right now, if I do that, it works perfectly and it generates the URL. But, as the {custom_domain} is not mapped to the platform, it gaves an error I know that, as a user in these kind of services like Bitly, you create a CNAME in your {custom_domain} DNSs to their Server IP. But I have no idea how to manage it from the other side, from the creator side. Because if I do that CNAME, the domain gives an error as is not configured in the Platform Thanks for your help!
Solution:
They have an Aliases option, the thing is that it doesn't seem the best option if the users can add their own domains and how to update that list "automatically". Thanks for the info, I will try it now manually with this test domain and find more info about it...
No description
Jump to solution
12 Replies
Dennis Koch
Dennis Koch3mo ago
But I have no idea how to manage it from the other side, from the creator side. Because if I do that CNAME, the domain gives an error as is not configured in the Platform
What's the error? I think by default all routes on a Laravel app should work for any domain unless you specify a specific domain I guess you might run into issues, as you have a wildcard on the root /{shortcode}
Ibon Azkoitia
Ibon Azkoitia3mo ago
Hello Dennis, thank you for your reply. Well, I'm not saying it's an error per se. I just don't know how to handle that situation. I will add realistic data so it's easier for examples: - Project domain: prolinks .pro - Project Filament Admin: prolinks .pro/app/ - Tenant Admin Url: prolinks .pro/app/ibontech - Tenant custom domain field: ibon .tech - Ibon .tech edit DNS A record to = Server IP where the project is located Situation 1 » Creating a url with the slug "dudvf0" - Example "prolinks .pro/dudvf0" (you can try it) - It works, it redirects to "productividad .pro" - Not the use case I'm looking for, I want users to use their own custom domain Situation 2 » Creating a url with the slug "dudvf0" - Example "ibon .tech/dudvf0" (you can try it) - It doesn't work, domain gives error "ERR_EMPTY_RESPONSE" Routes code for the fallback using the "prolinks .pro" base url before slug
Route::fallback(function (string $slug) {
$url = Link::where('slug', $slug)->firstOrFail();

if (!isset($url->url)) {
abort(404);
} else {
// Generate the final URL.
$final_url = LinkResource::getFinalUrl($url->url, $url->utm_source, $url->utm_medium, $url->utm_campaign_name);

return redirect($final_url);
}

});
Route::fallback(function (string $slug) {
$url = Link::where('slug', $slug)->firstOrFail();

if (!isset($url->url)) {
abort(404);
} else {
// Generate the final URL.
$final_url = LinkResource::getFinalUrl($url->url, $url->utm_source, $url->utm_medium, $url->utm_campaign_name);

return redirect($final_url);
}

});
public static function getFinalUrl($url, $utm_source, $utm_medium, $utm_campaign_name): string
{
$final_url = '';

if ($utm_source) {
$final_url = $url . '?utm_source=' . $utm_source;
}

if ($utm_medium) {
$final_url = $final_url . '&utm_medium=' . $utm_medium;
}

if ($utm_campaign_name) {
$final_url = $final_url . '&utm_campain=' . $utm_campaign_name;
}

if ( empty($final_url)) {
$final_url = $url;
}

return $final_url;
}
public static function getFinalUrl($url, $utm_source, $utm_medium, $utm_campaign_name): string
{
$final_url = '';

if ($utm_source) {
$final_url = $url . '?utm_source=' . $utm_source;
}

if ($utm_medium) {
$final_url = $final_url . '&utm_medium=' . $utm_medium;
}

if ($utm_campaign_name) {
$final_url = $final_url . '&utm_campain=' . $utm_campaign_name;
}

if ( empty($final_url)) {
$final_url = $url;
}

return $final_url;
}
I'm not an expert developer, so maybe I'm not explaining myself correctly. Thank you!
Dennis Koch
Dennis Koch3mo ago
Not sure about your server setup (nginx/apache), but you probably need to set a wildcard for the server if you haven't yet
Ibon Azkoitia
Ibon Azkoitia3mo ago
I'm using Laravel Forge + DigitalOcean In settings I see and it's activated the "Allow Wildcard Sub-Domains" but I don't see option for custom domains that users may add.
Dennis Koch
Dennis Koch3mo ago
I'm not using Forge so I am not sure whether they have an option for "Aliases".
Solution
Ibon Azkoitia
Ibon Azkoitia3mo ago
They have an Aliases option, the thing is that it doesn't seem the best option if the users can add their own domains and how to update that list "automatically". Thanks for the info, I will try it now manually with this test domain and find more info about it
No description
Ibon Azkoitia
Ibon Azkoitia3mo ago
It works! Without SSL for now » https://ibon.tech/dudvf0 https gives an error, will search more info about it Edit: now works with SSL also. will update this thread if I manage to automate things so other people can see it
Ibon Azkoitia
Ibon Azkoitia3mo ago
It seems that Forge creates a file so not configured domains do not work https://forge.laravel.com/docs/sites/the-basics.html#protecting-against-unconfigured-domains I moved the file to parent folder and removed the alias for the extra domain and still works. Will try to add another domain to test it if just with DNS change works. Also, if this works, I suppose have to add a functionality (on the route) to find if that domain is in the Database so I limit (me, not Forge) which domain works. Important if you are reading this: I'm not a developer, maybe I'm doing awful things.
The Basics | Laravel Forge
Deploy your Laravel PHP application painlessly
Ibon Azkoitia
Ibon Azkoitia3mo ago
So, it seams the option is to use the Forge API to add aliases: https://forge.laravel.com/api-documentation#add-site-aliases
API Documentation
Laravel Forge provides a rich HTTP API that allows you to programmatically provision and manage your PHP application servers.
Ibon Azkoitia
Ibon Azkoitia3mo ago
So, when a new Project is created, I suppose I can send a POST request to add the new custom_domain
Dennis Koch
Dennis Koch3mo ago
Would have proposed the same but thought you figured out how to change the server config.
Ibon Azkoitia
Ibon Azkoitia3mo ago
The thing is that I'm not expert with servers, but I defend myself with API. So I manage to make it work that way In the handleRegistration function I added the Http::put and worked fine