24

I'm having no luck converting htaccess rules to nginx rewrite. I've checked out the NginxRewriteModule documentation and have a few done, but the more complicate ones I'm at a loss for. Here's what I'm looking at:

RewriteRule    ^$                                               /cgi-bin/index.cgi [L]
RewriteRule    ([0-9A-Za-z]{12})-del-([0-9A-Za-z]+)/.+$         /cgi-bin/index.cgi?del=$1-$2 [L]

RewriteCond     %{REQUEST_FILENAME} !-f

RewriteRule    ^([0-9A-Za-z]{12})(\.html?|$)$                   /cgi-bin/index.cgi?op=download1&id=$1 [L]
RewriteRule    ^([0-9A-Za-z]{12})(\/.+|\.html?|$)               /cgi-bin/index.cgi?op=download1&id=$1&fname=$2 [L]
RewriteRule    ^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})$   /cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2 [L]
RewriteRule    ^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})(\/.+|\.html?|$)        /cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2&fname=$3 [L]

#RewriteRule    ^Reseller\.html$                                         /cgi-bin/Templates/Pages/english/Reseller.html [L]
RewriteRule    ^checkfiles\.html$                                       /cgi-bin/index.cgi?op=checkfiles [L]
RewriteRule    ^contact\.html$                                          /cgi-bin/index.cgi?op=contact [L]
RewriteRule    ^premium\.html$                                          /cgi-bin/index.cgi?op=payments [L]
RewriteRule    ^login\.html$                                            /cgi-bin/index.cgi?op=login [L]
RewriteRule    ^catalogue(.*)\.html$                                    /cgi-bin/index.cgi?op=catalogue&date=$1 [L]
RewriteRule    ^news([0-9]*)\.html$                                     /cgi-bin/index.cgi?op=news&page=$1 [L]
RewriteRule    ^n([0-9]+)-.*\.html$                                     /cgi-bin/index.cgi?op=news_details&news_id=$1 [L]
RewriteRule    ^free([0-9]+)\.html$                     /cgi-bin/index.cgi?op=registration&aff_id=$1 [L]
RewriteRule    ^users/([0-9A-Za-z\-_]{4,64})/?([0-9]+|$)        /cgi-bin/index.cgi?op=user_public&usr_login=$1&fld_id=$2 [L,NC]
RewriteRule    ^embedmp3-([0-9A-Za-z]{12})\.html$   /cgi-bin/index.cgi?op=mp3_embed&file_code=$1 [L]
RewriteRule    ^embedmp4-([0-9A-Za-z]{12})\.html$   /cgi-bin/index.cgi?op=mp32_embed&file_code=$1 [L]
RewriteRule    ^box$                                    /cgi-bin/index_box.cgi [L]

RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule    ^([0-9A-Za-z\-_]{4,64})(/[^\/]*/?|$)$            /cgi-bin/index.cgi?op=user_public&usr_login=$1&fld=$2 [L,NC]

RewriteCond     %{REQUEST_FILENAME} !-f
RewriteRule    ^([a-z0-9\-\_]+).html(.*)                        /cgi-bin/index.cgi?op=page&tmpl=$1$2 [L]
Mat
  • 188,820
  • 38
  • 367
  • 383
cheifops
  • 309
  • 1
  • 3
  • 6

5 Answers5

36

Online tools to translate Apache .htaccess to Nginx rewrite tools include:

Note that these tools will convert to equivalent rewrite expressions using if statements, but they should be converted to try_files. See:

Dave Jarvis
  • 28,853
  • 37
  • 164
  • 291
Alex Dean
  • 14,354
  • 11
  • 59
  • 72
  • 3
    this one didnt seem to be working for me but this did: http://winginx.com/htaccess – Hayden Thring Jan 13 '13 at 03:21
  • Neither did work for me. I tried a very basic rewrite rule. – Attila Fulop Jan 25 '13 at 09:15
  • 1
    can you tell me, where should I use of the output ? in the php file ? I used of [this](http://winginx.com/htaccess), and it gave me a output contained `if`, Now I want to know how should I use it ? –  Jul 08 '15 at 01:32
10

Have not tested it yet, but the looks are better than the one Alex mentions.

The description at winginx.com/en/htaccess says:

About the htaccess to nginx converter

The service is to convert an Apache's .htaccess to nginx configuration instructions.

First of all, the service was thought as a mod_rewrite to nginx converter. However, it allows you to convert some other instructions that have reason to be ported from Apache to nginx.

Note server instructions (e.g. php_value, etc.) are ignored.

The converter does not check syntax, including regular expressions and logic errors.

Please, check the result manually before use.

Community
  • 1
  • 1
Koen.
  • 21,087
  • 6
  • 75
  • 76
9

Rewrite rules are pretty much written the same way with nginx: http://wiki.nginx.org/HttpRewriteModule#rewrite

Which rules are causing you trouble? I could help you translate those!

Mathieu Rodic
  • 6,120
  • 2
  • 38
  • 47
3

Use this: http://winginx.com/htaccess

Online converter, nice way and time saver ;)

ErickXavier
  • 881
  • 2
  • 8
  • 24
2

You can easily make a Php script to parse your old htaccess, I am using this one for PRestashop rules :

$content = $_POST['content'];

    $lines   = explode(PHP_EOL, $content);
    $results = '';

    foreach($lines as $line)
    {
        $items = explode(' ', $line);

        $q = str_replace("^", "^/", $items[1]);

        if (substr($q, strlen($q) - 1) !== '$') $q .= '$';

        $buffer = 'rewrite "'.$q.'" "'.$items[2].'" last;';

        $results .= $buffer.PHP_EOL;
    }

    die($results);
Hassaan
  • 6,355
  • 5
  • 25
  • 44
Thomas Decaux
  • 18,451
  • 2
  • 83
  • 95
  • 12
    It's a shame a halfway decent answer was harmed by a flippant comment. I've edited it out. – Andrew Barber Nov 02 '12 at 17:13
  • This answer may be a lifesaver as a quick solution anyhow , but it's best not to rewrite like this. Especially for an e-commerce website. Please read this doc https://www.nginx.com/resources/wiki/start/topics/examples/likeapache-htaccess/ – Erdinç Çorbacı Jan 10 '17 at 01:17