Createurl Using Current Url As Base

(Not allowed to post links, so please assume all instances of domaincom is an actual domain)

Application hosted on domaincom


$this->createUrl( '/admin' );

User clicks admin link and is taken to domaincom/admin


$this->createUrl( '/products' );

User clicks product link and is taken to domaincom/admin/products instead of the expected domaincom/products

Strangely, on the staging server, when clicking on products, the user is taken to the expected domaincom/products url but when the code is pushed to production, we start seeing this issue.

I’ve asked my client to provide PHP version differences, so in the meantime, if someone knows what the issue might be here, please enlighten me.

The actions simply get data from model and render view - no redirections, etc

Layout code


<?php if ( ! Yii::app()->user->isGuest ): ?>

<nav id="main-nav">

    <ul class="l_tinynav1">


        <li<?php echo Yii::app()->controller->id === 'admin' ? ' class="active"' : ''; ?>>

            <a href="<?php echo $this->createUrl( '/admin' ); ?>">Home</a>

        </li>


        <li<?php echo Yii::app()->controller->id === 'products' ? ' class="active"' : ''; ?>>

            <a href="<?php echo $this->createUrl( '/products' ); ?>">Products</a>

        </li>


        <li<?php echo Yii::app()->controller->id === 'services' || Yii::app()->controller->id === 'faq' ? '     class="active"' : ''; ?>>

            <a href="#">Content</a>

            <ul style="display: block;">

                <li><a href="<?php echo $this->createUrl( '/services' ); ?>">Services</a></li>

                <li><a href="<?php echo $this->createUrl( '/faq' ); ?>">FAQs</a></li>

            </ul>

        </li>


        <li><a href="<?php echo $this->createUrl( '/admin/logout' ); ?>">Logout</a></li>


    </ul>

</nav>

<?php endif; ?>

Note

The servers use nginx proxy_pass directive to setup a reverse proxy

So I have to define requests[‘hostInfo’] in the config file like this:


'components' => array

(

    'request'=>array

(

        'hostInfo'=>'domaincom'

),

Might be related

After some debugging, I found something interesting. If I am on the /products/add page, createUrl will generate the right link for /admin. But if I am on the /admin page, createUrl will generate the incorrect link for /products/add. Following some further investigation, I found the these $_SERVER properties to be different.

When I’m on /admin


[SCRIPT_NAME] => /admin/index.php

[REQUEST_URI] => /admin/index.php

[DOCUMENT_URI] => /admin/index.php

When I’m on /products/add


[SCRIPT_NAME] => /index.php

[REQUEST_URI] => /products/add

[DOCUMENT_URI] => /index.php

It looks like it’s only an issue on first-level routes, /route will be an issue, but /route/action will not.

My nginx config


server {

        listen   80;


        set $host_path "/var/www/vhosts/app";

        server_name domain.int.com;


        access_log /var/www/vhosts/app/logs/access.log;

        error_log /var/www/vhosts/app/logs/error.log error;


        root   $host_path/htdocs;

        set $yii_bootstrap "index.php";


        charset utf-8;


        location / {

                index  index.html $yii_bootstrap;

                try_files $uri $uri/ /$yii_bootstrap?$args;

        }


        location ~ ^/(protected|framework|themes/\w+/views) {

                deny  all;

        }


        #avoid processing of calls to unexisting static files by yii

        location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {

                try_files $uri =404;

        }


        location ~ \.php$ {

		

		                fastcgi_split_path_info  ^(.+\.php)(.*)$;


                #let yii catch the calls to unexising PHP files

                set $fsn /$yii_bootstrap;

                if (-f $document_root$fastcgi_script_name){

                        set $fsn $fastcgi_script_name;

                }


                #

                root            $host_path/htdocs;

                fastcgi_pass    unix:/opt/app/php5/var/run/php-fpm.sock;

                fastcgi_index   index.php;


                #

                fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;

                include         fastcgi_params;

                fastcgi_param  PATH_INFO        $fastcgi_path_info;

                fastcgi_param  PATH_TRANSLATED  $document_root$fsn;

        }




        # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)

        location ~ /\. {

                deny all;

                access_log off;

                log_not_found off;

        }


}