Static HTML in Symfony

Symfony

I am working on a web project that uses Symfony as it’s framework. I have mod-rewrite enabled, so all the URL requests get routed through my application’s index.php. This means that¬† www.site.com/foo/bar is actually www.site.com/index.php/foo/bar and¬† my default routing.yml settings will look for a module named “foo”and an action named “bar” in my application.

The Problem

I want to host a couple of static web pages on my site independently of the rest of the Symfony driven pages. The first thing that came to mind was putting the files/folders in the project’s /web which happens to be my document root. Assuming that I created a¬† folder named “static” in /web and placed a index.html file in it, I can go to www.site.com/static/index.html and access the static html page, but¬† if all I wanted to do was to type¬† www.site.com/static/, I would get an error saying that your symfony project has no module named static.

The Solution

After several hours of trying a lot different things and a reasonable amount of googling even though I wasn’t sure of¬† what I was looking for, I found many solutions and most of them involved editing Symfony’s mod rewrite configuration so that it does not try to not rewrite requests coming to your static directory. This works but can be confusing for someone who does not know the syntax for mod rewrite so I kept on searching until I came across a more elegant/simple solution. All you need to do is create a .htaccess file inside your static folder with “RewriteEngine Off” and if you want “DirectoryIndex index.html” and all your problems are solved.

Some of the search terms I used were:

mod rewrite exclude directory
symfony redirect
symfony routing params redirect
symfony routing params
symfony routing static
apache directoryindex folder
subfolders symfony
subfolders symfony web directory

2 Responses to “Static HTML in Symfony”

  1. Baba Says:

    You can drop this line in your root .htacess file.. it should allow the /static path to run . With RewriteEngine On

    RewriteCond %{REQUEST_URI} !^/static/.*

  2. Dunkelheit Says:

    Works like a charm. Thanks a lot! :)

Leave a Reply