简体   繁体   中英

Symfony2 rewrite localhost/subdir/web/app.php/action to localhost/subdir/action

I'm try to use my Symphony 2 project without the /web slug in URL. I've added the following to the .htaccess file in my root folder (localhost/subdir).

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # Explicitly disable rewriting for front controllers
    RewriteRule ^web/app_dev.php - [L]
    RewriteRule ^web/app.php - [L]

    # Fix the bundles folder
    RewriteCond %{HTTP_HOST} ^localhost/subdir/$
    RewriteRule ^bundles/(.*)$ /web/bundles/$1  [QSA,L]

    RewriteCond %{HTTP_HOST} ^localhost/subdir/$
    RewriteCond %{REQUEST_FILENAME} !-f
    # Change below before deploying to production
    #RewriteRule ^(.*)$ /web/app.php [QSA,L]
    RewriteRule ^(.*)$ web/app_dev.php [QSA,L]
</IfModule>

This script loads my page, but It do not get any /web/bundle/mybundle/javascript/ , /images/ , or /css/ files. These are giving me a 404 error. Because in the <head> tag it says /bundle/mybundle/javascript/ without the /web prefix. The same is on web-profiler, debug toolbar etc.

How can I fix my .htaccess to make this work?

the web/ folder is ment to be the root web directory so you have a misconfiguration, tell your apache to have web as document root. in httpd.conf sth. like:

<VirtualHost *:80>
  ServerName name.localhost.com
  DocumentRoot "/Projects/name/web"
  <Directory "/Projects/name/web">
    Options Indexes FollowSymLinks
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

otherwise for example your parameters.yml with database pass and so on could be accessible.

the web/.htaccess conf for rewrite the app.php/app_dev.php in url could look ike :

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php




<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]    ##### this is the part that you     should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/app.php [L]        ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially

</IfModule>



<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the startpage 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM