3

How can I remove 'web/app_dev.php/' from my url when I want my symfony website to go live?

This is the default url during development,

http://{localhost}/my-symfony-2/web/app_dev.php/hello/World

So when I go live, I would like to be able to use this url to access my symfony website,

http://my-symfony-2.com/hello/World

Any ideas?

12
  • possible duplicate of How to remove "web/app_dev.php" from Symfony2 URLs? Commented Mar 19, 2015 at 13:11
  • I came across that one but it has no accepted answer. how can i know it is the correct answer then? Commented Mar 19, 2015 at 13:13
  • 3
    did you try any of provided solutions? Commented Mar 19, 2015 at 13:13
  • 2
    @oasis if you've access to the web root you can use these directives (only those which concern the removing a part of the url) on an .htaccess file also for the production site. Commented Mar 19, 2015 at 13:31
  • 1
    @oasis yes you need to add an .htaccess file. Now I can't, but I will write the answer later if someone doesn't. Commented Mar 19, 2015 at 13:57

2 Answers 2

3

To hide app.php you need to:

  1. having access at least to the web root of your site. Usually on the control panel of your web hosting space you can find from which you can upload your files (or if you have the access credentials you can install and use a Free FTP client like Filezilla).
  2. checking if you have the mod_rewrite module installed and enabled in Apache looking the phpinfo() under "apache2handler" ---> "Loaded Modules" directory (you should have that possibility directly through the control panel).

After these checks you have to:

NOTE: Symfony2 already comes with an .htaccess file stored in the default web directory but if you don't know what are you doing it's better to replace the directives containet within the "IfModule mod_rewrite.c" with those shown below.

  1. Use the native Symfony2 .htaccess or create a new one file with the .htaccess extension and copy/paste inside these directives to hide app.php in production (not on localhost):

    <IfModule mod_rewrite.c>
        RewriteEngine On  
        RewriteCond %{ENV:REDIRECT_STATUS} ^$  
        RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
        RewriteRule .? - [L]
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^(.*)$ app.php [QSA,L]
        RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
        RewriteRule ^(.*) - [E=BASE:%1]    
        RewriteRule .? %{ENV:BASE}app.php [L]
    </IfModule>
    
    # and from the symfony's original
    <IfModule !mod_rewrite.c>
        <IfModule mod_alias.c>
            # When mod_rewrite is not available, we instruct a temporary redirect of
            # the start page to the front controller explicitly so that the website
            # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
            # RedirectTemp cannot be used instead
        </IfModule>
    </IfModule>
    

    To apply the modifications you need to restart Apache (also this is usually did on the control panel of your web space).

Googling a bit you can find tons of examples and tutorials but to start to learn take a look at this simple .htaccess guide with many useful infos and examples and the Official URL Rewriting Guide (Apache2.4). If you encounter some problem update your post adding all related infos otherwise you can make another question here on SO.

Sign up to request clarification or add additional context in comments.

Comments

1

If you have access to the deployment server, from terminal you can use: php app/console cache:clear --env=prod and this will clear the prod cache and you will be able to use the standard route.

But if you are deploying the application on hosting where you haven't access to terminal, need to find the app/cache/* and app/logs/* and remove their content and after add the proper rights (766|777) for the folders.

2 Comments

thanks for the answer. i can't do command lines on the hosting server. I don't understand this bit - need to find the app/cache/* and app/logs/* and remove their content and after add the proper rights (766|777) for the folders.
after you copy all the code on the server, browse to folder app/cache/ and app/logs remove all the content in the folders, add read + write rights for the folders

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.