I think that what I'm trying to acomplish is similar to question (https://serverfault.com/questions/232051/rewrite-a-subdirectory-to-root-on-nginx#=) but I'll try to explain it better

so what I'm trying to do it to have the root of the domain (example.com) rewrite to the example.com/curr folder, except when curr is already in the request


example.com/ should rewrite to example.com/curr/

leading to the index.php located in there, serving it up (using php5-fpm for php)


example.com/curr should just be passed along, to be able to serve up the files


example.com/app should also just go to it's directory

current code (not working, 500 ISE):

location / {
  try_files $uri $uri/ v1/$uri/ /curr/index.php?q=$uri&$args;

#location ~* ^/([a-zA-Z0-9\-]+)$ {
#   root /webroot/www/myfolder/;
#   try_files /curr/$1;

location ^~ /p/ {
  rewrite ^/(.*)$ /curr/index.php?p=$1 last;

note: the last location here defines rewrite if a page is defined, this one works fine and should have a very similar syntax to what should work for the root dir

thanks for helping me out.

update: I've since also tried

location / {
  try_files @missing;

location /curr/{
  try_files $uri $uri/;

location @missing {
  rewrite ^ /curr/index.php?q=$request_uri&$args?;

according to the linked question something like this should work, but this just gives me the index page on root

Edit: I want to do this because all my subdomain folders are in the web/ folder, and I don't want those in the main website folder. I like to have everything separate & still conveniently accessible from f.e. FTP


I've got it up to the point where everything works, except 'example.com', which still renders me a 500 error


location / { rewrite ^ /curr/$uri last; }
location = / { rewrite ^ /curr/ last; }

location ^~ /curr/ {
  try_files $uri $uri/;

(please also note that using only location = / { rewrite ^ /curr/ last; } it did render the homepage correctly (but not everything else))

  • 1
    You forgot to say why you are trying to do this. This looks quite unusual, and it shouldn't really be necessary... – Michael Hampton Feb 17 '13 at 19:16

How about:

  location / {
     rewrite ^ /curr/ last;

Edit after discussion below:

#Default is to rewrite
location / { 
    rewrite ^ /curr$uri last; 

#Except if request already begins with curr...
location ^~ /curr/ {


#...OR it has a directory already (/curr/ will have matched above)
location ~ /(.*)/(.*) {


I am not convinced this is the best way to achieve whatever it is you're trying to achieve, but I think it should answer your question.

try_files won't achieve what you want I don't think (and not at all in your example as it's sending you into an infinite loop), as if index.html existed in the root, it wouldn't redirect you to /curr/ it would load up the index.html in root.

  • I've tried something similar already, this one is also giving me internal server error (500) edit: I've tried combining it with: location ^~ /v1/ { try_files $uri $uri/; } still renders 500 (and yes it's correctly in there) – Jonas D. Feb 17 '13 at 23:46
  • Sorry it should have been location = / { rewrite ^ /curr/ last; } – Alan Feb 18 '13 at 1:02
  • that did work for index (finally) but if I try to go to f.e.: example.com/js/something.js then it gives me a 404 not found edit: that's what the = is for right? (exact match => go to it) – Jonas D. Feb 19 '13 at 0:44
  • Yes, exact match. the /js/something.js shouldn't be written, you might want a location block for everything else though with your directives in there. – Alan Feb 19 '13 at 21:53
  • I did that, see original answer for current location blocks – Jonas D. Feb 20 '13 at 11:17

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.