Search code examples
phpsymfonyurlcentos5

Clean URL generator Symfony2 working in dev but not in prod


I'm using this code as a clean URL generator in Symfony 2.6:

namespace AppBundle\Utility;

class Utility {

    public static function getSlug($string, $separator = '-')
    {
        // Source: http://cubiq.org/the-perfect-php-clean-url-generator
        $slug = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
        $slug = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $slug);
        $slug = strtolower(trim($slug, $separator));
        $slug = preg_replace("/[\/_|+ -]+/", $separator, $slug);

        return $slug;
    }
}

It works fine in my dev environment, with special characters, but not in my prod environment (my server). It doesn't change characters like á é ñ %, they just disappear. Example "Artículo año 2000", it shows: "artculo-ao-2000".

I have installed PHP 5.5.23 in CentOS 5.11, I added UTF-8 as the default charset in http.conf for Apache and in php.ini, if I type "php app/check.php" everything is fine but it only suggests a PHP accelerator.

I tried uploading a file with a special character /holá.html, and it works fine.

I don't know what else to do. Is it because of the prod environment configuration?


Solution

  • iconv() requires correct setlocale();. Make sure you are not changing it between environments and, if your dev dev machine is different than prod, chack if you have the correct locales installed.