2

I'm trying to set up Symfony from an existing site that's on nginx. My local machine is Apache and this is the redirect scheme on nginx. How do I write this using RewriteRules?

location / {
               # try to serve file directly, fallback to rewrite
               try_files $uri @rewriteapp;
       }

       location @rewriteapp {
               # rewrite all to app.php
               rewrite ^(.*)$ /app.php/$1 last;
       }

       location ~ ^/(app|app_dev|config)\.php(/|$) {
               fastcgi_split_path_info ^(.+\.php)(/.*)$;
               include fastcgi_params;
               fastcgi_intercept_errors on;
               fastcgi_pass php_upstream; # Upstream on top of file
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               fastcgi_param HTTPS off;
       }

}
0
location / {
               # try to serve file directly, fallback to rewrite
               try_files $uri @rewriteapp;
       }

       location @rewriteapp {
               # rewrite all to app.php
               rewrite ^(.*)$ /app.php/$1 last;
       }

The first part seems to be the only rewrite. This looks like it could be written as follows using mod_rewrite on Apache in the root .htaccess file:

# Enable trailing pathname information (if not already)
AcceptPathInfo On

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /app.php/$0 [L]

Internally rewrite all requests that don't map to physical files or directories to /app.php/<requested-url-goes-here>.

Since this uses trailing path information on the URL (/$0), you need to make sure that the server permits this, otherwise you'll get an immediate 404 Not Found. However, this generally is enabled by default (although it does depend on the handler).

  • This will result in a redirect loop – Michael Aug 23 '16 at 11:29
  • It shouldn't - app.php presumably exists as a physical file? The above rewrite only occurs if the requested file does not exist. – MrWhite Aug 23 '16 at 11:31
  • It is a local file – Michael Aug 23 '16 at 12:56
  • A "local file" in the document root I assume? Just to add, you will also need to ensure that trailing path information is also permitted (it should be by default) - I've updated my answer. – MrWhite Aug 24 '16 at 15:28
  • Yes. Calling example.com/app_dev.php/user/new is working now, but example.com/user/new not – Michael Aug 24 '16 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.