Symfony2 - How can I remove 'web/app_dev.php/' from my url?

后端 未结 2 560
终归单人心
终归单人心 2021-01-14 06:20

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://{loca         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 06:59

    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):

      
          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]
      
      
      # and from the symfony's original
      
          
              # 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
          
      
      

      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.

提交回复
热议问题