Giving your symfony apps better urls

I love pretty URLs. I don’t like file name extensions unless they end with .html.

All versions of symfony save me from address bar ugliness, but only for the first application I create. Everything else is ass.php/backwards.

I’m going to give you two ways of beautifying your second, third, and forth applications so you can http://www.get.your/application/looking/sexy. Both methods involve creating a subdirectory.

Put it all in a subdirectory, and symlink your way out

So if you have an app named “admin,” you can do the following:

  1. Create a directory in your web folder called admin
  2. Copy your admin.php and admin_dev.php into this folder
  3. Copy the .htaccess file into this folder
  4. Rename admin.php to index.php
  5. Edit the file now named index.php, and change the require_once statement at the top so it’s an extra directory back
  6. Symbolically link the js, css, images, and uploads directories from within the admin directory to the directory above; so from your app directory: ln -s ../js

This is what I usually do in a Linux environment. However, I’m developing on a Windows box, and symbolic links don’t translate easily between Windows and Linux. So here’s the second option, without using symbolic links:

Put just the .htaccess in a subdirectory

  1. As before, create a subdirectory in your web folder for the admin.
  2. Copy the .htaccess file in, and at the very bottom of the file, change index.php to ../admin.php. It should read RewriteRule ^(.*)$ ../admin.php [QSA,L]
  3. Edit your routing.yml file for the admin app, and append /admin/ to all of your routes.

That should about cover it. Also remember to set no_script_name to on in each of your applications.

Enjoy.

Alternative methods? Please leave comments.

3 Comments

  1. Posted October 3, 2009 at 6:17 pm | Permalink

    I usually do it only modifying the .htaccess adding a rule before the last one like this

    RewriteCond %{HTTP_HOST} ^backend\.(.*)$
    RewriteRule ^.*$ backend.php [QSA,L]

    This one is for subdomains backend.mysite.com but it could be also modified for /backend

    • Rodrigo
      Posted October 21, 2009 at 5:23 pm | Permalink

      Could you please type the rule for directory version?? http://www.mysite.com/admin
      Thanks in advance.

      • Rodrigo
        Posted October 22, 2009 at 11:55 am | Permalink

        I answer myself.

        Replace
        # no, so we redirect to one of our web controllers
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]

        With

        # no, so we redirect to one of our web controllers
        # 1) /admin => backend controller
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^admin(.*)$ backend.php [QSA,L]

        # 2) other => frontend controller
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]


Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*