0

So my problem is kinda weird, it only occurs when I test the application offline (on my PC with WampServer), the same code works 100% correctly online.

Here is how I use the helper (just example):

<a href="<?php

echo $this->url(array('module' => 'admin',
                      'controller' => 'index',
                      'action' => 'approve-photo',
                      'id' => $this->escape($a->id),
                      'ret' => urlencode('admin/index/photos')),
                null,
                true);

                            ?>" class="blue">approve</a>

Online, this link works great, it goes to the action which looks similar to this:

public function approvePhotoAction()
{
    $request = $this->getRequest();
    $photos = $this->_getTable('Photos');

    $dbTrans = false;

    try {

        $db = $this->_getDb();
        $dbTrans = $db->beginTransaction();

        $photos->edit($request->getParam('id'),
                      array('status' => 'approved'));

        $db->commit();

    } catch (Exception $e) {
        if (true === $dbTrans) {
            $db->rollBack();
        }
    }

    $this->_redirect(urldecode($request->getParam('ret')));
}

So online, it approves the photo and redirects back to the URL that is encoded as "ret" param in the URL ("admin/index/photos").

But offline with WampServer I click on the link and get 404 error like this:

Not Found

The requested URL /public/admin/index/approve-photo/id/1/ret/admin/index/photos was not found on this server.

What the hell?

I'm using the latest version of WampServer (WampServer 2.0i [11/07/09]). Everything else works.

Here is my .htaccess file, just in case:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

# Turn off magic quotes
#php_flag magic_quotes_gpc off

I'm using virtual hosts to test ZF projects on my local PC. Here is how I add virtual hosts.

httpd.conf:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName myproject
    DocumentRoot "C:\wamp\www\myproject"
</VirtualHost>

the hosts file:

127.0.0.1    myproject

Any help would be appreciated because this makes testing and debugging projects on my localhost a nightmare and almost impossible task. I have to upload everything online to check if it works :(

UPDATE:

Source code of the _redirect helper (built in ZF helper):

/**
 * Set redirect in response object
 *
 * @return void
 */
protected function _redirect($url)
{
    if ($this->getUseAbsoluteUri() && !preg_match('#^(https?|ftp)://#', $url)) {
        $host  = (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'');
        $proto = (isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']!=="off") ? 'https' : 'http';
        $port  = (isset($_SERVER['SERVER_PORT'])?$_SERVER['SERVER_PORT']:80);
        $uri   = $proto . '://' . $host;
        if ((('http' == $proto) && (80 != $port)) || (('https' == $proto) && (443 != $port))) {
            $uri .= ':' . $port;
        }
        $url = $uri . '/' . ltrim($url, '/');
    }
    $this->_redirectUrl = $url;
    $this->getResponse()->setRedirect($url, $this->getCode());
}

UPDATE 2:

Output of the helper offline:

/admin/index/approve-photo/id/1/ret/admin%252Findex%252Fphotos

And online (the same):

/admin/index/approve-photo/id/1/ret/admin%252Findex%252Fphotos

UPDATE 3:

OK. The problem was actually with the virtual host configuration. The document root was set to C:\wamp\www\myproject instead of C:\wamp\www\myproject\public.

And I was using this htaccess to redirect to the public folder:

RewriteEngine On

php_value upload_max_filesize 15M
php_value post_max_size 15M
php_value max_execution_time 200
php_value max_input_time 200
# Exclude some directories from URI rewriting
#RewriteRule ^(dir1|dir2|dir3) - [L]

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ /public/index.php [NC,L]

Damn it I don't know why I forgot about this, I thought the virtual host was configured correctly to the public folder, I was 100% sure about that, I also double checked it and saw no problem (wtf? am I blind?).

5
  • could you post the code for $this->redirect? Commented Dec 20, 2009 at 21:07
  • $this->_redirect is a built in helper in the Zend Framework but I included the source from ZF in my post. Commented Dec 20, 2009 at 21:18
  • look at my default%2Fstatic%2Ffaq and yours /admin%252Findex%252Fphotos your url is encoded twice Commented Dec 20, 2009 at 22:26
  • Yep but it still works. I don't have time to go and remove the urlencode from all views now. It would take hours so I will just leave it like that, in case it stops working in the future again I will just use urldecode twice :) Commented Dec 20, 2009 at 22:30
  • you can write ActionHelper to check if ret param exist - urldecode it ;) and write and continue to write the correct code in ActionControllers. no need to double urldecode ;) Commented Dec 20, 2009 at 22:40

1 Answer 1

1
  1. check your php.ini
    php_flag magic_quotes_gpc off Must be Off at online and local servers

  2. do not use urlencode 'ret' => urlencode('admin/index/photos')) because url helper has fourth parameter $encode = true and it is true by default

  3. show us the output of $this->url(array('module' => 'admin'..... at server and at localhost

i'm using wamp at home and it works like charm

UPDATE 1
try $this->_redirect( '/'. ltrim(urldecode($request->getParam('ret')), '/') );

UPDATE 2;

ViewScript

    <a href="<?php

echo $this->url(array('module' => 'default',
                  'controller' => 'index',
                  'action' => 'test-encoded-redirect',
                  'ret' => urlencode('default/static/faq')),
            null,
            true);

                        ?>">test me</a>


    <?php
    class IndexController extends Smapp_Controller_Action 
    {   

        public function testEncodedRedirectAction()
        {

                            // ddump($this->_getAllParams());
            // ddump output 
            // Array ( 
            //  [controller] => index 
            //  [action] => test-encoded-redirect 
            //  [ret] => default%2Fstatic%2Ffaq 
            //  [module] => default 
            // )


            $ret = '/' . ltrim(urldecode($this->_getParam('ret')), '/');
            // echo $ret;
            // output: /default/static/faq

            $this->_redirect($ret);
        }
    }

it works!

Sign up to request clarification or add additional context in comments.

9 Comments

Magic quotes are off on both local and web server. Chech the end of my post.
And the urlencode() is not the problem. If I get rid of it the issue persists. Plus it's an older application and I would have to remove that urlencode() in dozens of views (in older ZF versions the fourth parameter was not there I think so they made sure old ZF apps would not break).
:) ok. show the echo ltrim(urldecode($request->getParam('ret')), '/') output
it can be baseUrl problem. because final link shold looks like /public/ret/admin/index/photos at localhost and /admin/index/photos at server
if so - u must reconfig your WAMP VirtualHost to point 'DocumentRoot "C:\wamp\www\myproject\public"`
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.