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 need help converting some Apache .htaccess rules to nginx ones. Hope someone around more experienced could help me. Here are my rules:

1

    RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.url.ba [NC]
RewriteRule ^(.*)$ http://url.ba/$1 [R=301,L]

RewriteRule ^api\.php /index.php?c=api&m=index&%{QUERY_STRING} [L]

RewriteRule ^contact\.php /index.php?c=contact&m=index&%{QUERY_STRING} [L]

RewriteRule ^([a-zA-Z0-9]{4,25})$ /index.php?c=api&m=check&hash=$1 [L]

RewriteCond $1 !^(index\.php|images|css|script|ZeroClipboard\.swf)
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) / [R=301,L]

2

    RewriteEngine on


RewriteCond %{HTTP_HOST} ^www.img.ba.ba$ [NC]
RewriteRule ^(.*)$ http://img.ba/$1 [R=301,L]


RewriteCond $1 !(\/protected)$
RewriteRule ^(.*)\/protected$ /protected.php?hash=$1 [L]


RewriteCond $1 !(thumb\.php)$
RewriteRule ^(.*)\/thumb$ /thumb.php?hash=$1 [L]


RewriteCond $1 !^(index\.php|thumb\.php|upload\.php|contact\.php|protected\.php|api\.php|password\.php|favicon\.ico|images|css|script)
RewriteRule ^(.*)$ /index.php?hash=$1 [L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) / [R=301,L]
share|improve this question

2 Answers 2

Since you have the rules ready, it's easy. Here are the guidelines

  • Read the rewrite docs
  • Read the core variables (%{HTTP_HOST} becomes $host, for example)
  • RewriteCond will be turned into if blocks
  • RewriteRules are pretty much the same, except the variable differences and the usage of regular expressions. Also, flags are written in full like break, permanent and so on.

There are many examples on the rewrite page.

share|improve this answer

Most apache rewrite conditions should not be turned into if blocks. Those conditions matching HTTP_HOST should be turned into server blocks, those matching the uri or request filename should be replaced with location blocks, and those testing existence of files should be replaced with try_files. Please don't use if blocks where they're not necessary. Assuming that all dynamic requests are supposed to go through php, I think the correct translation of those rules is:

server {
  server_name www.url.ba;
  rewrite ^ http://url.ba$request_uri? permanent;
}

server {
  server_name url.ba;

  root /path/to/root;

  location = /api.php { rewrite ^ /index.php?c=api&m=index; }
  location = /contact.php { rewrite ^ /index.php?c=contact&m=index; }

  location ~ "/([a-zA-Z0-9]{4,25})$" /index.php?c=api&m=check&hash=$1?; }

  location /index.php {
    fastcgi_split_path_info (/index.php)(.*);
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_pass your_backend;
  }

  location /images { try_files $uri @home; }
  location /css { try_files $uri @home; }
  location /script { try_files $uri @home; }
  location /ZeroClipboard.swf { try_files $uri @home; }
  location @home { rewrite ^ / permanent; }

  location / {
    rewrite ^ /index.php$uri;
  }
}

server {
  # Did you really mean .ba.ba?
  server_name www.img.ba.ba;
  rewrite ^ http://img.ba$request_uri? permanent;
}

server {
  server_name img.ba;

  # I'm not positive that these translations are correct,
  # I'm guessing that the repeated $1 is always the requested
  # uri in the apache rules

  # The (?<variable> capture syntax is 0.8.25+.  If you're
  # running an older version, the easiest way will be to
  # repeat the location regex in the rewrite and capture $1

  location ~ ^/(?<hash>.*)/protected$ {
    rewrite ^ /protected.php?hash=$hash;
  }

  location ~ ^/(?<hash>.*)/thumb\.php$ {
    rewrite ^ /thumb.php?hash=$hash;
  }

  location ~ ~/(index|thumb|upload|contact|protected|api|password)\.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_pass your_backend;
  }

  location /images { try_files $uri @home; }
  location /css { try_files $uri @home; }
  location /script { try_files $uri @home; }
  location @home { rewrite ^ / permanent; }

  location / { rewrite ^/(.*) /index.php?hash=$1; }
}
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.