currentlyOffline

Custom URLs in Zend Framework (v1.6.1)

If you are having problems with custom URLs in Zend Framework – you’re not alone.

Recently I was rewriting one of my websites using ZF. The site is old, well ranked in search engines. In a situation like this you don’t really want to change URL structure. So i started to play with Apache rewrite rules and got stuck.

The problem is, that by default HTTP request object is using $_SERVER['REQUEST_URI'] which means that even after setting rule like

RewriteRule ^articles/(.*)$ articles/view/url/$1 [L]

won’t work, because $_SERVER['REQUEST_URI'] remains unchanged.

To fix this, I’ve changed Http.php in library/Zend/Controller/Request/ and on line 391(should be the same if you are using same version as me, or look for public function setRequestUri($requestUri = null) ) and replaced

389:            if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
390:                $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
391:            }elseif (isset($_SERVER['REQUEST_URI'])) {
394:                $requestUri = $_SERVER['REQUEST_URI'];
395:            }elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI

with:

389:            if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
390:                $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
391:            }elseif(isset($_SERVER['PATH_INFO'])){
392:                     $requestUri = $_SERVER['PATH_INFO'];
393:            }elseif (isset($_SERVER['REQUEST_URI'])) {
394:                $requestUri = $_SERVER['REQUEST_URI'];
395:            } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI

It’s a quite dirty hack because you have to change core files and need to remember that before updating ZF to a new version, but it does the job.

If you know a better solution – please let me know!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

This entry was posted on Tuesday, November 11th, 2008 at 8:54 pm and is filed under Zend Framework. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

« Zend Framework – A table must have a primary key, but none was found
Square brackets in HTML element name – Zend Framework »

One Response to “Custom URLs in Zend Framework (v1.6.1)”

  1. currentlyOffline » Blog Archive » 1and1 hosting fun Says:
    June 25th, 2009 at 9:05 pm

    [...] I care about URL’s much, to make search engines happy mostly I’m using this trick for custom URL’s with Zend Framework And the reason why it stopped working is that 1and1 is not giving you $_SERVER['PATH_INFO'] any [...]

Leave a Reply

  • Pages

    • About
  • Archives

    • December 2009
    • November 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • February 2009
    • January 2009
    • November 2008
  • Categories

    • books (3)
    • Cristina Scabbia (1)
    • I hate computers… and Internet! (6)
    • Jokes (1)
    • jQuery (1)
    • Music (1)
      • italian metal (1)
      • Lacuna Coil (1)
    • Question – Answer (1)
    • randomness (3)
    • Uncategorized (2)
    • web hosting jokes (3)
    • Zend Framework (6)

currentlyOffline is proudly powered by WordPress | Bob