Serving backend from a subpath of frontend: problem with pretty URLs

Hi, Yii2 users and developers.

Starting using Yii2, I encountered some problem for serving an application with backend prefixed by a subpath.

I did not found similar topic in this forum, so started a new thread

– although I found similar question in the wiki (www.yiiframework.com/wiki/755/how-to-hide-frontend-web-in-url-addresses-on-apache/#c18264).

I ask here for help for serving with Apache an application, from the advanced template (github.com/yiisoft/yii2-app-advanced), with the fronted served at the root of a domain, e.g.


sub.domain.tld/

, and the backend served at a prefixed URL path, e.g.


sub.domain.tld/admin

, with pretty URLs enabled (www.yiiframework.com/doc-2.0/guide-runtime-routing.html#using-pretty-urls). I didn’t found problem with serving frontend, but serving backend with pretty URLs leads to a problem (described hereafter the configuration).

Here’s the whole configuration:

  • application stands in the filesystem at

/var/www/sub.domain.tld

, with its whole backend/, frontend/, common/, console/, environments/ and vendor/ directories;

  • both frontend and backend applications are configured for using pretty URLs:

// @backend/config/main.php

// @frontend/config/main.php

$config = [

    // ...

    'components' => [

        // ...

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'enableStrictParsing' => false,

            'rules' => [

            ],

        ],

    ],

    // ...

];

  • Apache’s configuration stands in a dedicated

/etc/apache2/sites-available/sub.domain.tld.conf

, with a single virtualhost:


<VirtualHost *:80>

        ServerName sub.domain.tld

 

        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/sub.domain.tld/frontend/web

 

        ErrorLog ${APACHE_LOG_DIR}/sub.domain.tld-error.log

        CustomLog ${APACHE_LOG_DIR}/sub.domain.tld-access.log combined

 

        <Directory "/var/www/sub.domain.tld">

                Order deny,allow

                Deny from all

        </Directory>

 

        # Backend

        Alias "/admin" "/var/www/sub.domain.tld/backend/web"

        # Alternatively, we could use AliasMatch directive instead:

        #AliasMatch "^/admin/(.*)$" "/var/www/tp5/backend/web/$1

        <Directory "/var/www/sub.domain.tld/backend/web">

                # use mod_rewrite for pretty URL support

                RewriteEngine on

 

                # If a directory or a file exists, use the request directly

                # -- for assets.

                RewriteCond %{REQUEST_FILENAME} !-f

                RewriteCond %{REQUEST_FILENAME} !-d

 

                # Otherwise forward the request to index.php

                RewriteRule . index.php

 

                # use index.php as index file

                DirectoryIndex index.php

 

                Order deny,allow

                Allow from all

        </Directory>

 

        # Frontend

        <Directory "/var/www/sub.domain.tld/frontend/web">

                # use mod_rewrite for pretty URL support

                RewriteEngine on

 

                # If a directory or a file exists, use the request directly

                # -- for assets.

                RewriteCond %{REQUEST_FILENAME} !-f

                RewriteCond %{REQUEST_FILENAME} !-d

 

                # Otherwise forward the request to index.php

                RewriteRule . index.php

 

                # use index.php as index file

                DirectoryIndex index.php

 

                Order deny,allow

                Allow from all

        </Directory>

</VirtualHost>

As said before, serving the frontend doesn’t generate problem. Nor does serving the backend with its SiteController without its predefined behaviors (github.com/yiisoft/yii2-app-advanced/blob/master/backend/controllers/SiteController.php#L18).

Nor does with configuration of default URLs scheme.

Problem occurs when requesting


sub.domain.tld/admin/

, with 1) these SiteController’s behaviors enabled, 2) pretty URLs configured. Request receives a 302 Response with new location set to


sub.domain.tld/admin/site/login

. But this second request is then managed by frontend’s script, as tell $_SERVER[‘SCRIPT_FILENAME’] set to ‘/var/www/sub.domain.tld/frontend/web/index.php’ ($_SERVER[‘REDIRECT_URL’] is set to ‘/var/www/tp5/backend/web/index.php’). It finally falls in a 404 error, as I suppose the frontend failed to manage ‘admin/site’ route.

var_dumping values did not help: @web alias is well set to ‘/admin’, $application->request->baseUrl to value of @web, $application->request->scriptUrl to ‘/admin/index.php’

I could not imagine where the error stands: in Apache’s configuration? in UrlManager’s configuration? in SiteController’s behaviors/access filters? Your help is strongly welcome!

Please excuse for the poor english’s writing (not my mother tongue), and thank you for reading this whole post :slight_smile: