TransWikia.com

Converting nginx to Apache rewrites

Server Fault Asked on November 27, 2021

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;
       }

}

One Answer

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

Answered by MrWhite on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP