Search code examples
.htaccesssymfonysubdomain

Symfony 3 routing subdomains


I have to bundles, frontend and backend. I want the subdomain "admin.domain.com" to work as "admin.domain.com/whateverpage" and controlled by the backend bundle controllers. All other subdomains should work like "sub1.domain.com/{dynamicwhateverpage}" or "sub2.domain.com/{dynamicwhateverpage}" and be controlled by the frontend bundle controllers.

I got it to work with "admin.domain.com/backend/whateverpage" and "sub1.domain.com/frontend/{dynamicwhateverpage}". The backend and frontend in the URL of course should not be visible to the users. Is there a way to rewrite with htaccess? Or is there even a better way to achieve this kind of rewriting?


Solution

  • Symfony can match routing based on host. This should also work when importing routes, though I have not tested it;

    /**
     * @Route("/", name="project_page", host="{project}.example.com")
     */
    public function projectPageAction($project) {
        // ...
    }
    

    Just make sure your none dynamic routes are loaded before the all capturing one, or make sure the project one can't match eg. admin.

    Earlier Routes Always Win

    Why would you ever care about requirements? If a request matches two routes, then the first route always wins.