Nice urls with symfony


Symfony | Technical | Development | July 15, 2010

Usually when working with symfony, when the time comes to deploy the application, one problem arises, the urls.

While developing a normal url could be: http://www.jnieto.org/web/index.php/articles/my_first_article

Two elements are not welcome here: index.php and web/

 

Index.php/

To get rid of index.php a setting must be configure in the settings.yml file from the config folder:

 

 
prod:
  .settings:
    no_script_name:         true
 

 

Now after cleaning the cache the annoying index.php is gone.

Note: Just an application can have this setting as true (more info in this regard in the symfony guide )

 

Web/

This can be avoided depending on the kind of hosting available. If it is a dedicated server then the solution is to change the webroot to be the actual web dir. Ok that's the easy one, however when using a shared hosting some steps should be followed.


First add to the factories.yml file of the desire application the following code:

 
  controller:
    class:                   FrontWebController
 

 

Then create the class FrontWebController.class.php in the project/lib/controller folder (it could be other folder but this follows the symfony original folder structure).

The class should overwrite the method genUrl:

 
  public function genUrl($parameters = array(), $absolute = false)
  {
    $url = parent::genUrl($parameters, $absolute);
 
    if(preg_match('/\.php/', $url) ||
       preg_match('/index\.php/', $_SERVER['SCRIPT_NAME']))
 
      $url = substr($url, 0, strpos($url, 'web')).
             substr($url, strpos($url, 'web') + 4);
 
    return $url;
  }
 

 

This method helps the link_to's to generate urls without the usual .../web/...

Don't forget to clean the cache for symfony to take account of the changes.

 

Now copy the .htaccess file to the root folder and copy this code:

 

 
  Options +FollowSymLinks +ExecCGI
 
 
  RewriteEngine On
 
  # we skip all files in /web
  RewriteCond %{REQUEST_URI} ^/web/
  RewriteRule .* - [L]
 
  # we rewrite all other files with .something to /web
  RewriteCond %{REQUEST_URI} \..+$
  RewriteCond %{REQUEST_URI} !\.html$
  RewriteRule ^(.*)$ /web/$1 [L]
 
  # we check if the .html version is in /web (caching)
  RewriteRule ^$ /web/index.html [QSA]
  RewriteRule ^([^.]+)$ /web/$1.html [QSA]
  #####################################################
 
  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ /web/index.php [QSA,L]
 

 

This code gets rid of the undesired web/, now the site could be accessed by:

http://www.jnieto.org/articles/my_first_article

A nice and clean url.

 

 

References:

http://forum.symfony-project.org/index.php/m/102500/#msg_102500

http://groups.google.com/group/symfony-devs/browse_thread/thread/0ec9a54b46725beb?pli=1

 


Comments


Keven said about about 1 month later

In facts, the web root must link to the /web directory, not the root of the symfony project.
If you have a URL like http://host/web/xxx, then some of your data are exposed (e.g. http://host/config/*.yml).

Keven said about about 1 month later

...and remember to delete frontend_dev.php, the data of your server are exposed to.

nugsheeve said about 8 months later

hi, new to the site, thanks.

Cibbutcouri said about about 1 year later

pharmacy college in faridabad http://sundrugstore.net/products/xtz--energy-booster-.htm pharmacy abbreviations

Quarargully said about about 1 year later

pharmacy in aruba http://sundrugstore.net/products/septilin.htm pharmacy right to refuse

Quarargully said about about 1 year later

pharmacy technician schools mn http://sundrugstore.net/products/zyloprim.htm cvs pharmacy nashville indiana

phothTow said about about 1 year later

international pharmacy reviews http://sundrugstore.net/products/coumadin.htm foxton pharmacy

DicHoishiono said about about 1 year later

indiana pharmacy tech testing http://sundrugstore.net/products/metoclopramide.htm georgia state board of pharmacy

DicHoishiono said about about 1 year later

international pharmacy guide http://sundrugstore.net/products/lipitor.htm cvs pharmacy 30305

phothTow said about about 1 year later

fefe and spain and pharmacy http://sundrugstore.net/products/provigrax.htm haw river pharmacy hours

lipinfideRen said about about 1 year later

best pharmacy prices http://sundrugstore.net/products/fml-forte.htm offshore pharmacy

Scievese said about about 1 year later

searchengines tenuate25mgs online pharmacy with no prescription http://legalexclusivepharmacy.com/catalog/Blood_Pressure/Capoten.htm why choose a career in pharmacy

satplors said about about 1 year later

elephant pharmacy http://legalexclusivepharmacy.com/catalog/Blood_Pressure/Plendil.htm pharmacy job opening



Hey Stranger, leave a comment:

Last Posts

Autoloading: Symfony vs Yii

Symfony2, PhpBB4 and Drupal8

Type and boolean columns with Doctrine and Symfony

Refactoring queries with Doctrine

Extending your Doctrine Model: Template Classes

Integrating Doctrine: Symfony vs Yii

Passing parameters from the action to the view: Symfony vs Yii

Yii framework

Adding custom information to your Doctrine schema

PHP Advent Calendar 2010


My Tweets

2 months ago
Interesting tips about #scaling http://t.co/QC2paoDK
2 months ago
Learning to use #Windows #ActiveDirectory
3 months ago
Ayni - Blog: CUESTIONARIOS PARA ANÁLISIS-http://localhost:8080/AYNI-war/faces/ListarComentario.xhtml?txtIdPublicacion=2
3 months ago
Día tranquilo en casa :)#fbb
4 months ago
#Adobe Reader App crushed my new #Android movile :/
4 months ago
Testing from Android =D
5 months ago
#Refactoring code.
5 months ago
You should Snog, Marry or Avoid me http://t.co/pZCDIwW
5 months ago
Yahoo’s Options v@TechCrunchnhttp://t.co/yjzsKstKst What will Yahoo do?
5 months ago
Just realize my post http://t.co/nSuJjSp, written so many time ago, really contribute to make it happen: http://t.co/wMqYrV3
5 months ago
An Introduction to Redirecting URLs on an Apache Server http://t.co/s79ThOy via @WebmasterWorld
5 months ago
Actualizando a #Eclipse Indigo!
6 months ago
Vota x lamula.pe: http://t.co/WTWMkFG via @addthis
6 months ago
Apple Pushes Past Exxon To Become The Most Valuable Public Company In The World (Temporarily) via@TechCrunchhhttp://t.co/lowONSZZ
7 months ago
finally this class #semester is finished, new #projects in mind
7 months ago
Mark Zuckerberg Explains His Law Of Social Sharing [Video]http://t.co/sqq10Ehh via@TechCrunchh
8 months ago
retomando mi #twitter #notime
8 months ago
How Facebook Can Put Google Out of Businesshttp://t.co/HqDfQGoo via@TechCrunchh
9 months ago
So why not just cut out the middle man? Microsoft.http://techcrunch.com/2011/05/15/samsung-series-5-chromebook/
9 months ago
really like to #design class hierarchies with #compositepattern
9 months ago
Similarly, Microsoft.com started to use jQuery instead of their own ASP.NET Ajax framework. They are still using Windows, for whatever XD
9 months ago
Estudiantes de la PUCP le “voltean” campaña a esposo de Keiko | yoperiodhttp://t.co/ruI9UnmI9Unm@lamulaamula
9 months ago
Reading: Apress.-.Pro.PHP.Application.Performance.2010 - Very Insteresting #php #performance #read
9 months ago
debug_backtrace() is very important on certain situations. #php #debug
10 months ago
@alvarezrodrich felicitaciones!
10 months ago
time to do some #uml diagrams, #classdiagram
10 months ago
hoy es el día,#votaa conciente#peruu
10 months ago
@skoop @funkatron I think someone had a bad day!, #Frameworks are there but you don't have to use them.
10 months ago
making #wireframes for a new #functionality
10 months ago
so much #spam on my #blog =(
10 months ago
learning new topics and tools that I did not use before #rcp
10 months ago
aprendiendo muchos temas y herramientas que no utilizaba antes #rcp
11 months ago
#tweaking httpd.conf #virtualhost
11 months ago
thanks #symfony 1.4, even when i'm not using the entire #framework, yours classes save my life!
11 months ago
integrating with #SOA using #soap
11 months ago
It seems my most #productive working hours are on #sunday #afternoon #evening! XD
11 months ago
#ASOT 500 =)
11 months ago
installing SCA_SDO on #Centos #php
11 months ago
My web service using #soap worked!!!!! #php #SCA #SDO
11 months ago
Working in a new place since last week!, #RCP: Red Científica Peruana, the one which sells the .pe domains in#Perúú ->#happypy
11 months ago
Finally with a new #laptop: #Toshiba =)
11 months ago
@pasku1 Thanks, I will try Pivotaltracker.
11 months ago
@doolphy thanks for your answer doolphy! I'll try you!
11 months ago
@jmasson thanks for your answer! Jira + Confluence is a good combination.
11 months ago
Which is the best project management and collaboration tool right now? #projectmanagement #tool #collaboration
11 months ago
What a #voice! Sied Van Riel feat Nicole McKenna - Stealing Time (Aly & Fila Remix) + #ASOT 497 #trancefamily
11 months ago
working on a situation where #php #traits would be very useful
12 months ago
It is #awesome when you finish doing a lot of changes and nothing is broken =) #TDD #testing
12 months ago
oh, happy birthday! @mtabini o mejor dicho feliz cumpleaños!
12 months ago
why do #IE8 not accept #javascript "const" keyword?
12 months ago
Discovering there is much #more to do with #javascript ... a lot.
12 months ago
#composition over #inheritance: #javascript
12 months ago
Awesome #song!: Cerf, Mitiska & Jaren - Another World (Original Vocal Mix) #ASOT 495 #trancefamily
12 months ago
My legal woman is #PHP, but I have an affair with #Javascript, overall when she wears #jQuery.
12 months ago
This presentation is one of the best I've seen about #unit #testing http://www.slideshare.net/avalanche123/clean-code-5609451
12 months ago
where to do a master on #IT: US or Spain? #survey #php #master plz RT
12 months ago
Bobina feat. Betsie Larkin - You Belong To Me: What a #beautiful voice -> #ASOT 494 #arminvanbuuren
12 months ago
OH NO, IT'S MONDAY -- 2011-02-07 http://t.co/pgaIxe5 via @gojkoadzic
12 months ago
it was not a + b, it was parserInt(a) + parseInt(b) =(, #javascript #fail
about 1 year ago
The models are complete representations of the system, whereas an #architectural #view focuses only on what is architecturally #significant.
about 1 year ago
@jmasson that would be great and finally #wikimedia, #drupal, #wordpress and maybe #joomla would push towards the same side, the #php side.
about 1 year ago
@jmasson Thanks!, #PHP has a bright #future ahead.
about 1 year ago
A new #blog post about not reinventing the #wheel: http://www.jnieto.org/article/symfony2_phpbb4_and_drupal8 #symfony #phpbb #drupal
about 1 year ago
@giorgiosironi #indeed, that's a very good #question. I think an average of 4 but also depends on how much that #developer work.
about 1 year ago
It seems the new platform for deploying, managing and scaling PHP apps is http://orchestra.io/ #cool
about 1 year ago
#Phase project planning vs #iteration project #planning - #project #management
about 1 year ago
#jeditable with #jquery save my life =)
about 1 year ago
Amazing #song -> Sied van Riel feat. Nicole McKenna - Stealing Time #ASOT 493 #trancefamily
about 1 year ago
I simply love "offset" #jquery function =)
about 1 year ago
I really don't understand why projects like #drupal does not base their components in projects like #doctrine and #symfony
about 1 year ago
OH NO, IT'S MONDAY -- 2011-01-17 http://t.co/37pr4Bd via @gojkoadzic
about 1 year ago
@alvarezrodrich me alegra ver q ya borró su cuenta Sr. Rodrich,#twitterr es malo jajja, XD
about 1 year ago
Acabo de hacerle a mi #brother @diegonl89 un blog para que hable de #actualidad en general: http://www.elgatotechero.com #peru
about 1 year ago
I just made to my #brother a #blog to talk about current #events in #peru: http://www.elgatotechero.com
about 1 year ago
Amazing things can be done with #javascript and #css, and of course with the help of #jQuery =)
about 1 year ago
An architecturally significant element is an element that is important for #understanding the #system.
about 1 year ago
An architecturally element has a wide impact on the #structure, #performance, #robustness, #evolvability, and #scalability of a #system.
about 1 year ago
@giorgiosironi Definitely!
about 1 year ago
Playing with #table #inheritance in #Doctrine
about 1 year ago
Reading about #RUP, and how addresses the #major difficulties in a new #project.
about 1 year ago
Yandex in 2010: 43 percent revenue growth http://t.co/cpjT5Jw via @cnet
about 1 year ago
Going forward!!!!! =) poco a poco llegan los resultados de tanto esfuerzo #fb
about 1 year ago
Perfect #system with respect to the #requirements but the #wrong system with respect to the #real #problem at the time of #delivery.
about 1 year ago
Going #forward! =) #fb
about 1 year ago
@sam_dark Ok thanks!, but I don't understand why in #Yii documentantion use $_GET and $_POST instead of CHttpRequest http://bit.ly/i5emoL