Search code examples
phpapache.htaccessdnszend-framework2

How to use subdomain route with zf2 ?


I am trying to use subdomain route in zf2, but getting dns error. I have added a route like this :

'testSubDomain' => array(
                        'type'    => 'Hostname',
                        'options' => array(
                                'route'    => ':blog.mydomain.net',
                        ),
                        'may_terminate' => false,
                        'child_routes' => array(
                                'home' => array(
                                        'type' => 'Literal',
                                        'options' => array(
                                                'route'    => '/',
                                                'defaults' => array(
                                                        'controller' => 'Application\Controller\Solution',
                                                        'action'     => 'testSubdomain'
                                                )
                                        )
                                )
                        )
                ),

I have created apache vhost and the configurations of /etc/apache2/sites-enabled/000-default.conf are as:

<VirtualHost *:80>

        ServerName mydomain.net
        ServerAlias mydomain.net
        DocumentRoot /var/www/beta/public

        <directory /var/www/beta/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all

        </directory>

        <Directory /var/www/beta/public>
                Options -Indexes
                Require all granted
        </Directory>
</VirtualHost>
----------
Some other vhost
-----------

When I go to blog.mydomain.net it is showing server DNS address could not be found. I haven't defined any vhost for subdmoain in 000-default.conf so it should use the first one. I have also added one A record for the subdomain but it is also not working.


Solution

  • The solution posted in this question is correct, somethings need to keep in mind. Don't create a Vhost for your subdomain, but your subdomain should point to the serer. If you create a vhost for subdomain then it will follow the appplication modules config route and show you the homepage.

    'testSubDomain' => array(
                            'type'    => 'Hostname',
                            'options' => array(
                                    'route'    => 'blog.mydomain.net',
                            ),
                            'may_terminate' => false,
                            'child_routes' => array(
                                    'home' => array(
                                            'type' => 'Literal',
                                            'options' => array(
                                                    'route'    => '/',
                                                    'defaults' => array(
                                                            'controller' => 'Application\Controller\Solution',
                                                            'action'     => 'testSubdomain'
                                                    )
                                            )
                                    )
                            )
                    ),
    

    When you go to blog.mydomain.net it will show you the content of testSubdomain function. No need to create any vhost for subdomain.