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:
- Create a directory in your web folder called admin
- Copy your admin.php and admin_dev.php into this folder
- Copy the .htaccess file into this folder
- Rename admin.php to index.php
- Edit the file now named index.php, and change the require_once statement at the top so it’s an extra directory back
- 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
- As before, create a subdirectory in your web folder for the admin.
- 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] - 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
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
Could you please type the rule for directory version?? http://www.mysite.com/admin
Thanks in advance.
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]