views:

300

answers:

2

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?).

+1  A: 
  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!

SM
Magic quotes are off on both local and web server. Chech the end of my post.
Richard Knop
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).
Richard Knop
I tried that, the same problem.
Richard Knop
:) ok. show the `echo ltrim(urldecode($request->getParam('ret')), '/')` output
SM
it can be `baseUrl` problem. because final link shold looks like `/public/ret/admin/index/photos` at localhost and `/admin/index/photos` at server
SM
if so - u must reconfig your WAMP `VirtualHost` to point 'DocumentRoot "C:\wamp\www\myproject\public"`
SM
admin/index/photos but that's from the admin/index/photos controller action (i.e. page with the approve link)... I cannot test anything in the admin/index/approve-photo action because the only thing that gets printed on the screen is the error.
Richard Knop
The virtual host is configured properly. The document root is the public directory (otherwise the application wouldn't work at all on localhost... and it works fine, just the approve link and similar links with encoded ret parameter don't work). I mentioned that in the OP.
Richard Knop
The other thing I suspected could be the problem was different versions of ZF online and offline but I have just downloaded the exact ZF from the web server and replaced the current ZF offline with the downloaded contents. The problem is still there so that wasn't the case.
Richard Knop
The only thing that comes to mind is that Apache is differently configured online and offline. But I don't have idea what besides mod_rewrite could be the problem and that is enabled at both places.
Richard Knop
See my updated post. Thanks a lot :)
Richard Knop
A: 

Just tried both On and Off on line #744 in /etc/php.ini.default

magic_quotes_gpc = On

Then ran restarts both ways: sudo apachectl -k restart

urlencoded strings still do not work. Bummer.

doublejosh