简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM