How to remove “web/app_dev.php” from Symfony2 URLs?

前端 未结 7 971
误落风尘
误落风尘 2020-12-13 07:01

I just created my first Symfony2 project. But the \"/web/app_dev.php\" part in the URL annoys me. It should be possible to do this without Virtual hosts...

相关标签:
7条回答
  • 2020-12-13 07:41

    If you use an apache virtual host, you can get it working the way you desire. Here is an example virtual host from my xampp:

    <VirtualHost *:80>
        ServerName myurl.local
    
        # Basic stuff
        DocumentRoot "C:/path/to/symfony/web"
        DirectoryIndex app.php
        <Directory "C:/path/to/symfony/web">
            AllowOverride All
            Allow from All
        </Directory>
    </VirtualHost>
    

    After restarting your apache, you can access the project through http://myurl.local (don't forget to configure your hosts file under C:\Windows\system32\drivers\etc\hosts!). If you want to have the dev environment (app_dev.php) not showing up in the url, change the DirectoryIndex to app_dev.php.

    0 讨论(0)
  • 2020-12-13 07:44

    Symfony2 comes with a built in debug mode, which is what you are using when you access url's with the app_dev.php addition. The debug mode caches significantly less than the production mode which can be accessed by directing your browser to the url, but leaving out the app_dev.php. If accessing this url doesn't work then it probably means that you need to clear your production cache.

    To clear the cache direct you terminal (command console) to the root of you Symfony project. From there type the following command:

    php app/console cache:clear --env=prod --no-debug
    

    Per comments below in Symfony 3 this has moved:

    php bin/console cache:clear --env=prod --no-debug
    

    This should clear your productions cache and allow you to access urls without the app_dev.php.

    As for the web part of the url. The easiest way to remove that is to set the web root of your project to the web folder. It's best to do this with apache using virtual hosts, but there are ways to do it with the htaccess.

    This link explains how to use the htaccess file to change the webroot. http://kb.siteground.com/how_to_change_my_document_root_folder_using_an_htaccess_file/

    Hope this helps!

    0 讨论(0)
  • 2020-12-13 07:44
    1. //enable mod rewrite
      sudo a2enmod rewrite

    2. Change all occurrences of AllowOverride None by AllowOverride All
      File: (/etc/apache2/apache2.conf) or (/etc/apache2/sites-available/000-default.conf) - for me work with /etc/apache2/apache2.conf file.

    Example:

    <Directory /var/www/web/>
            # enable the .htaccess rewrites
            Options Indexes FollowSymLinks
            #AllowOverride None #commented
            AllowOverride All
            Require all granted
    </Directory>
    
    1. //Restart apache service:
      sudo service apache2 restart
    0 讨论(0)
  • 2020-12-13 07:47

    you just need to copy the content of .htaccess file to the your-website.conf

    inside the TAG Directory

    0 讨论(0)
  • 2020-12-13 07:48

    You would need .htaccess file in parent folder of web, however, I wouldn't suggest doing so. That folder, apart from web, contains complete source, binaries, vendors and much more therefore you would need to apply many rules just to allow web-access to web folder and disallow web-access to everything else.

    So, it's doable, but I would definitely go for vhost solution...

    0 讨论(0)
  • 2020-12-13 08:04
    <VirtualHost *:80>
        DocumentRoot "path_to_symfonyTest/web"  
        ServerName "symfonyTest"  
        <Directory "path_to_symfonyTest/web">
            DirectoryIndex app_dev.php
            AllowOverride All
            Order allow,deny
            Allow from all
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app_dev.php [QSA,L]
            RedirectMatch permanent ^/app_dev\.php/(.*) /$1
        </Directory>
    </VirtualHost>
    

    That's part of my httpd.conf

    0 讨论(0)
提交回复
热议问题