Help converting Apache HTACCESS directives to nginx?

Hi, I’m brand new to nginx and I am trying to convert the app I wrote
over from Apache as I need the ability to serve a lot of clients at once
without a lot of overhead! I’m getting the hang of setting up nginx and
FastCGI PHP but I can’t wrap my head around nginx’s rewrite format just
yet. I know you have to write some simple script that goes in the server
{} block in the nginx config but I’m not yet familiar with the syntax.
Could anyone with experience with both Apache and nginx help me convert
this to nginx format? Thanks!

Options +FollowSymLinks
RewriteEngine On

# ------------------------------------------------------ #
# Rewrite from canonical domain (remove www.)            #
# and from beta subdomain (remove beta.)                 #
# ------------------------------------------------------ #

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

  RewriteCond %{HTTP_HOST} ^beta.domain.com
  RewriteRule (.*) http://domain.com/$1 [R=301,L]

# ------------------------------------------------------ #
# This is for maintenance mode (if /msg.html exists)     #
# ------------------------------------------------------ #

  # Rewrite everything else and attempt a TwitStamp
  # Not a file, directory, or file.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{DOCUMENT_ROOT}/$1.php !-f
  RewriteCond %{DOCUMENT_ROOT}/msg.html -f
  RewriteRule ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$
generation/image.php?user=${escapemap:$1}&template=${escapemap:$2}
[NC,L]

  # Redirect everything to maintenance page
  # Not msg.html, not image.php, not from my IP
  RewriteCond %{REQUEST_URI} !/msg.html$
  RewriteCond %{REQUEST_URI} !/generation/image.php$
  RewriteCond %{REMOTE_HOST} !^76\.178\.84\.13
  RewriteCond %{REMOTE_HOST} !^76\.19\.200\.16
  RewriteCond %{DOCUMENT_ROOT}/msg.html -f
  RewriteRule $ /msg.html [NC,L]

# ------------------------------------------------------ #
# This redirects index.php to /                          #
# ------------------------------------------------------ #

  RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index|index\.php)\ HTTP/
  RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L]

# ------------------------------------------------------ #
# This rewrites 'directories' to their PHP files,        #
# fixes trailing-slash issues, and redirects .php        #
# to 'directory' to avoid duplicate content.             #
# ------------------------------------------------------ #

  RewriteCond %{DOCUMENT_ROOT}/$1.php -f
  RewriteRule ^(.*)$ $1.php [L]

  RewriteCond %{DOCUMENT_ROOT}/$1.php -f
  RewriteRule ^(.*)/$ http://domain.com/$1 [R=301,L]

  RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.]+\.php\ HTTP/
  RewriteCond %{DOCUMENT_ROOT}/$1.php -f
  RewriteRule ^([^.]+)\.php$ http://domain.com/$1 [R=301,L]

# ------------------------------------------------------ #
# If it wasn't redirected previously and is not          #
# a file on the server, rewrite to a TwitStamp           #
# ------------------------------------------------------ #

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$
generation/image.php?user=${escapemap:$1}&template=${escapemap:$2}
[NC,L]

Posted at Nginx Forum:

if ($http_host ~ "^www.domain.com"){
  set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
  rewrite /(.*) http://domain.com/$1 permanent;
  break;
}
if ($http_host ~ "^beta.domain.com"){
  set $rule_1 1$rule_1;
}
if ($rule_1 = "1"){
  rewrite /(.*) http://domain.com/$1 permanent;
  break;
}
if (!-f $request_filename){
  set $rule_2 1$rule_2;
}
if (!-d $request_filename){
  set $rule_2 2$rule_2;
}
if (!-f $document_root/$1.php){
  set $rule_2 3$rule_2;
}
if (-f $document_root/msg.html){
  set $rule_2 4$rule_2;
}
if ($rule_2 = "4321"){
  setenv ${escapemap:$1}&template;
  rewrite ^/([a-z0-9_\-@#\ /"'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$;
}
if ($uri !~ "/msg.html$"){
  set $rule_3 1$rule_3;
}
if ($uri !~ "/generation/image.php$"){
  set $rule_3 2$rule_3;
}
#ignored: condition 2
#ignored: condition 3
if (-f $document_root/msg.html){
  set $rule_3 5$rule_3;
}
if ($rule_3 = "541"){
  rewrite /$ /msg.html last;
}
#ignored: condition 0
  rewrite ^/(index|index\.php)$ http://domain.com/ permanent;
  break;
if (-f $document_root/$1.php){
  set $rule_5 1$rule_5;
}
if ($rule_5 = "1"){
  rewrite ^/(.*)$ /$1.php last;
}
if (-f $document_root/$1.php){
  set $rule_6 1$rule_6;
}
if ($rule_6 = "1"){
  rewrite ^/(.*)/$ http://domain.com/$1 permanent;
  break;
}
#ignored: condition 0
if (-f $document_root/$1.php){
  set $rule_7 2$rule_7;
}
if ($rule_7 = "21"){
  rewrite ^/([^.]+)\.php$ http://domain.com/$1 permanent;
  break;
}
#ignored: condition 0
#ignored: condition 1
  setenv ${escapemap:$1}&template;
  rewrite ^/([a-z0-9_\-@#\ /"'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$;

Posted at Nginx Forum:

On Sun, Aug 22, 2010 at 10:55:50AM -0400, st1905 wrote:

}
if (!-f $document_root/$1.php){
set $rule_3 1$rule_3;
rewrite /$ /msg.html last;
if (-f $document_root/$1.php){
if ($rule_7 = “21”){
rewrite ^/([^.]+).php$ http://domain.com/$1 permanent;
break;
}
#ignored: condition 0
#ignored: condition 1
setenv ${escapemap:$1}&template;
rewrite ^/([a-z0-9_-@#\ /"'+]+)/?([a-z0-9_-]+)?(.png|/)?$;

[/code]

No, you should not use this sendmail style configuration.
However, I do not understand what the original htaccess should do.


Igor S.
http://sysoev.ru/en/