How to enable custom domains in PHP

I have a system in which users can enter their purchased domain into their profile, so when accessing their domain, it must replace its own domain, for example.

http://domain.com/custom-name to http://purchaseddomain.com .

Therefore, when they access their purchase domain, he must transfer them to his profile , including his navigation links, for example, the links on their page will be replaced by their acquired domain, for example, viewing their records will be:

http://domain.com/custom-name/records to http://purchaseddomain.com/records .

Tumblr allows this function, however I have no idea how this all works:

enter image description here

That is how I like to have such a function, I was looking for SO, but it didn't seem to help.

Now this is a problem, I'm not sure how I can verify, confirm and combine the purchased domain on my server without problems using PHP. For this I use Codeigniter.

Is there a robust, stable plugin / library or a detailed guide that might be able to include user domains masking the internal domain?

My server runs Ubuntu 11.10 on nginx 1.0.6.

The templates will be wonderful for me, what can I do - all I need help with is securely accept and merge my domain into my server.


EDIT: just looked at nginx VirtualHostExample , it looks good, but how can I dynamically add / remove this domain while the domain has an A record pointing to my server?

+4
source share
3 answers

You do not merge your domain into your server.

In fact, when they register their domains, they will point to your server.

In your server configuration, you will have to dynamically create rules that implicitly redirect the page to the page created on your server.

So, users will see http://purchaseddomain.com/on-uri , but you will serve the page http://domain.com/custom-name/one-uri

that is: you like it if you added it to .htaccess - even if you are not using apache, just explain what a β€œsystem” is:

 RewriteCond %{HTTP_HOST} purchaseddomain\.com$ [NC] RewriteRule (.*) /custom-name/$1 
+2
source

I know that almost ten years have passed since this question was asked, but I managed to find a solution. The key to this (at least in Symfony) is to use the host parameter in your routes and redirect to the correct controller. In my case, I defined a route:

 custom_domain: path: /{any} host: '{host}' controller: App\Controller\CustomDomainController::handle requirements: any: '.*' host: '^(?!yourmaindomain\.com)' 

Responsibility for intercepting any request lies in any way, if it comes from a user domain.

Inside the controller, I map the user domain to the user slug. In the code below, it is hard-coded in the property, but it can be obtained from the cache, database, or anything else. Then I prepare the path as if the request was made using the main domain (so instead of john-doe.com/abc it is yourmaindomain.com/john-done/abc ) to find the right controller to handle the request.

 <?php declare(strict_types=1); namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RouterInterface; class CustomDomainController extends AbstractController { private $slugs = [ // This is the thing you type in tumblr assosiated with your username slug 'john-doe.com' => 'john-doe', ]; private $router; public function __construct(RouterInterface $router) { $this->router = $router; } public function handle(Request $request): Response { // Here the host is john-doe.com $host = $request->getHost(); // We get his slug, so john-doe $slug = $this->slugs[$host]; // We prepare path info /john-doe/whatever-he-typed-after-domain // john-doe.com/abc would result in /john-doe/abc $pathinfo = rtrim("/{$slug}{$request->getPathInfo()}", '/'); // Here we create a context for route matching // Probably it would be better to create new context // Not to mess with an existing one, remember we get this by reference $context = $this->router->getContext(); // We want to find a route that can handle our desired path // So yourmaindomain.com/john-doe/abc $context->setHost('yourmaindomain.com'); $routeCollection = $this->router->getRouteCollection(); $urlMatcher = new UrlMatcher($routeCollection, $context); try { $match = $urlMatcher->match($pathinfo); } catch (ResourceNotFoundException|MethodNotAllowedException $e) { throw $this->createNotFoundException(); } // Now we have a controller that can handle the request // Match object contains also all request parameters // If the route was /{something}/{else} // The match array contains those values return $this->forward($match['_controller'], $match); } } 

Since we forward here, rather than redirecting the URL, it remains unchanged. I also think that the routes are recompiled on every request, this is something that may need to be optimized, but hey, it works !: D Hope this helps someone :)

0
source

This is what works for me:

 server { server_name *.mydomain.com root /var/www/$host; ... } 

Then you need to create directories, for example: /var/www/user1.mydomain.com /, / var / www / user2.mydomain.com /, ...

I could not figure out how to leave ".mydomain.com" from the directory name. If anyone has any ideas, pls let me know :)

-one
source

Source: https://habr.com/ru/post/1393655/


All Articles