Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have the below .htaccess file in apache that redirects all request to app/index.php with or without a GET parameter. I just installed NGINX and my application won't run. Can anyone assist in replicating same functionality in NGINX? Thanks.

RewriteEngine on    

Options -Indexes

RewriteRule    ^$ app/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ app/index.php?url=$1 [QSA,L]
share|improve this question

migrated from stackoverflow.com Jul 16 '12 at 7:27

This question came from our site for professional and enthusiast programmers.

2 Answers 2

Use the rewrite module. Something like this should do it:

location / {
  autoindex off;

  rewrite ^/$ app/ last;
  if (!-e $request_filename) {
    rewrite ^/(.*)$ app/index.php?url=$1;
  }
}
share|improve this answer

Use try files

There is no need to use the rewrite module for most of that config.

The apache rewrite rules mean:

  • RewriteRule ^$ app/ [L] - send the url / to the app dir
  • RewriteCond %{REQUEST_FILENAME} !-d - if the request is not a dir
  • RewriteCond %{REQUEST_FILENAME} !-f - if the request is not a file
  • RewriteCond %{REQUEST_FILENAME} !-l - if the request is not a symlink
  • Then send the request to app/index.php?url=$1

In nginx the equivalent is:

# No automatic directory listings
autoindex off;

# Send the root url to the app dir
index index.php;
rewrite ^/$ app/ last;

# check if the url matches a file, directory and if not send to app/index.php
try_files $uri $uri/ app/index.php?url=$uri;

The check for a symlink is superfluous since a symlink is also a file or directory.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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