RewriteRule in Apache with Symfony2 not removing app.php

前端 未结 7 1995
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 15:34

I have the following .htaccess file in my web directory for my Symfony2 installation:


    RewriteEngine On
    RewriteCond %{R         


        
相关标签:
7条回答
  • 2020-12-13 16:14

    I had this problem today and the fix was to append a /$1 at the end of the url rewrite rule.

    My .htaccess looks like this:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ app.php/$1 [QSA,L]
    </IfModule>
    

    And the virtual host is defines as this:

    <VirtualHost *:80>
       DocumentRoot /var/www/html/symfony_site/web
       DirectoryIndex app.php
       <Directory /var/www/html/symfony_site/web >
           AllowOverride All
           Allow from All
       </Directory>
    </VirtualHost>
    
    0 讨论(0)
  • 2020-12-13 16:16

    I managed to remove web/app.php part on my shared server.

    1. First I moved app.php out of the web directory one step above thus my app.php on the root directory. Then I placed a .htaccess on the root directory
    RewriteEngine on
    
    RewriteBase /
    
    RewriteRule ^css/(.*) web/css/$1
    
    RewriteRule ^images/(.*) web/images/$1
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
    

    I used RewriteCond on css and images as they still lie inside /web/ directory so all request to css and images will be routed to web/css and web/images directories respectively.

    It worked well for me.

    0 讨论(0)
  • 2020-12-13 16:17

    When you write RewriteCond %{REQUEST_FILENAME} !-f you say "Don't change real files... they should still be reachable." If you remove that, it should work.

    The reason for it allowing that is some frameworks let you use http://site.tld/script.php/abc=123&def=456 style of links.

    If you want it to allow filenames except app.php, you could add that as another RewriteCond.

    0 讨论(0)
  • 2020-12-13 16:29

    We've been struggling with that as well. There are two solutions:

    1. set document root of your virtual host to point directly to the /web/ subfolder (as per Symfony2 guidance)
    2. move app.php and app_dev.php outside of the /web/ subfolder, then update .htaccess and include paths in both files accordingly (i.e. remove web/ to correct all paths after you've manually moved the files one folder above)

    (Note: cannot provide examples from vanilla installation of S2, since symfony.com seems to be down at the moment.)

    0 讨论(0)
  • 2020-12-13 16:32

    I just got the same issue and I fixed it like this:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /web/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ app.php [QSA,L]
    </IfModule>
    
    0 讨论(0)
  • 2020-12-13 16:33

    That rewrite rule is not meant to remove app.php from the URL. It's purpose is to use app.php for every request, if a request URL doesn't correspond to a real file. Since app.php is a real file, it will be used to serve a request.

    If you want get rid of web/app.php part, first create a virtual host pointing to the web folder:

    <VirtualHost *:80>
        ServerName whatever
        DocumentRoot /path/to/project/web
    </VirtualHost>
    

    This will remove the web part.

    Then, to remove the app.php part, add this to the beginning of the web/.htaccess file:

    RedirectMatch permanent ^/app\.php/(.*) /$1
    
    0 讨论(0)
提交回复
热议问题