Home > Enterprise >  convert nginx to htaccess(apache)
convert nginx to htaccess(apache)

Time:03-27

I got the following rewrite rules in Nginx in which im trying to convert for apache/.htaccess rules in which i was keep getting errors and didn't work out well, didnt find any online convertor from nginx to .htaccess only the opposite, Im hoping if anyone could help me out converting it.

This is my nginx rules:

if (!-d $request_filename){ 
 set $rule_0 1$rule_0; 
} 
if (!-f $request_filename){ 
 set $rule_0 2$rule_0; 
} 
if ($rule_0 = "21"){ 
 rewrite ^/(.*?)([^/]*)$ /$1index.php?$2 last; 
}

this is my .htaccess try:

<IfModule mod_rewrite.c>
RewriteEngine on
# Apache 2.4
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*?)([^/]*)$ $1index.php?$2 [QSA,PT,L]
RewriteEngine On
            RewriteBase /
        if (!-d $request_filename){ 
 set $rule_0 1$rule_0; 
} 
if (!-f $request_filename){ 
 set $rule_0 2$rule_0; 
} 
if ($rule_0 = "21"){ 
 rewrite ^/(.*?)([^/]*)$ /$1index.php?$2 last; 
}

ErrorDocument 404 /404.php

</IfModule>

but i keep on getting parse error whenever i try to do this, anyone have experience to sort this out?

CodePudding user response:

You appear to have already done it, but for some reason you have also embedded the original Nginx directives in the middle (which will naturally result in a parse error).

So, the equivalent Apache/.htaccess directives would seem to be the following only:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*?)([^/]*)$ $1index.php?$2 [QSA,L]

The PT flag is not required in a .htaccess context.

You do not need the <IfModule> wrapper, unless these directives are optional.


if (!-d $request_filename){ 
 set $rule_0 1$rule_0; 
}

However, the first set directive implies that the $rule_0 variable might have already been set earlier in the config?

  • Related