Path of assets in CSS files in Symfony 2

Path of assets in CSS files in Symfony 2

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Path of assets in CSS files in Symfony 2

Handling relative paths to images and fonts within a url() directive inside a CSS stylesheet is a persistent pain point in Symfony 2 when the application runs behind a virtual host or a subdirectory. The core issue arises because the browser resolves URLs relative to the domain root, not the current file location, while your development environment often places assets at specific filesystem paths like /app_dev.php.

When you structure your project with static files in src/MyCompany/MyBundle/Resources/assets and images in web/images, referencing an image directly as url("../images/myimage.png") works only if the CSS file itself is served from a directory immediately adjacent to the images. However, once you move that CSS into a build pipeline or a bundle resource directory, the relative path calculation breaks.

Consider this common scenario: your application runs at https://example.com/app_dev.php. If your CSS file is loaded via web/css/stylexyz.css and your image sits in web/images, a relative path of ../images/myimage.png technically points to the correct location if the web server serves the files from the root. But Symfony 2's development environment often serves the entire bundle context under /app_dev.php. Consequently, the browser interprets the relative path as /app_dev.php/images/myimage.png, triggering a NotFoundHttpException.

The first instinct is to use absolute paths like url("../../../../web/images/myimage.png"). This fails immediately in production or subdirectory deployments because it hardcodes the internal filesystem structure into the delivered asset. You cannot ship an application that relies on knowing its own directory depth relative to the web root; that breaks the principle of portable deployment.

A popular approach involves using the Assetic bundle with the cssrewrite filter. The theory is sound: you define paths relative to your source directory in your Twig template, and let Assetic rewrite them before dumping the file. You might configure your Twig block like this:

{% stylesheets
    '@MyCompanyMyBundle/Resources/assets/css/*.css'
    filter="cssrewrite"
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

In your CSS, you would write something like url("../../../../web/images/myimage.png") to point to the actual location. The problem is how Assetic handles the rewriting logic. When using the default configuration, Assetic often attempts to resolve the path relative to the bundle resource directory rather than the web root. This results in the output path being recalculated incorrectly, often pointing back into the Resources/assets folder or generating a path like ../../Resources/assets/ instead of the intended /web/images/myimage.png.

Even after running assetic:dump, the generated CSS might contain paths like url("../../../web/images/myimage.png"). This is still brittle because it assumes a specific directory structure between the output and the static files. If you move your web folder or change the bundle structure, the asset links break.

The third solution often employed is to make CSS files relative to their own location (url("../images/myimage.png")). While this works in production where the web server serves files from a flat public directory, it fails catastrophically in the development environment where Symfony serves the application under /app_dev.php. The browser sees /app_dev.php/css/stylexyz.css, adds the relative path, and ends up looking for /app_dev.php/images/myimage.png, which does not exist.

The robust solution requires decoupling the asset resolution logic from the directory depth of the deployment. Instead of relying on ../ or hardcoded ../../web/..., you should leverage Symfony's asset helpers to generate URLs that are independent of the internal directory structure. In a modern Symfony setup (or even legacy Assetic configurations), the goal is to ensure that when the browser requests the CSS, the subsequent image requests resolve correctly regardless of whether the app is at / or /app_dev.php.

The most reliable pattern involves using asset() in Twig to generate the base URL for your static files. By configuring the bundle to map the web directory correctly and ensuring your CSS uses paths relative to the mapped asset root (e.g., url("images/myimage.png")), you ensure that the browser resolves the path against the domain root. This works because the Twig template generates a link tag that points directly to the public URL, effectively flattening the navigation tree for the client.

If you are stuck on Symfony 2 with Assetic, ensure your assetic:dump configuration explicitly maps the base path of your static files so that the filter does not try to calculate relative jumps back into the source tree. The key is to treat the web directory as the root for asset resolution in the final output, rather than trying to navigate from the bundle resource folder to it manually. This approach guarantees that subdirectory deployments and production environments behave identically without requiring manual path adjustments.

Our network