2011-04-30 118 views
23

我沒有運氣將htaccess規則轉換爲nginx重寫。我已經檢查了NginxRewriteModule文檔,並完成了一些,但是我更加複雜的是我不知所措。下面是我在看: http://wiki.nginx.org/HttpRewriteModule#rewrite將htaccess轉換爲nginx

哪些規則是給你造成麻煩:

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] 
+0

更好地爲http://serverfault.com/或http://webmasters.stackexchange.com/。 – 2013-03-25 00:56:05

回答

9

重寫規則非常寫nginx的以同樣的方式?我可以幫你翻譯這些!

33

在線工具翻譯的Apache的.htaccess到Nginx的重寫工具包括:

注意,這些工具將使用if語句轉換成等價rewrite expressions,但他們應該轉換爲try_files。請參閱:

+3

這一個似乎沒有爲我工作,但這樣做:http://winginx.com/htaccess – 2013-01-13 03:21:13

+0

都沒有爲我工作。我嘗試了一個非常基本的重寫規則。 – 2013-01-25 09:15:10

+1

你能告訴我,我應該在哪裏使用輸出?在PHP文件?我使用了[this](http://winginx.com/htaccess),它給了我一個包含'if'的輸出,現在我想知道該如何使用它? – 2015-07-08 01:32:06

1

您可以輕鬆地做出一個PHP腳本來分析你的舊htaccess的,我用這一個的Prestashop規則:

$內容= $ _ POST [ '內容' ]。

$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); 
+12

這是一個恥辱,一個不錯的答案受到了輕浮評論的傷害。我編輯了它。 – 2012-11-02 17:13:59

+0

無論如何,這個答案可能是一個快速解決方案,但最好不要像這樣重寫。特別是對於電子商務網站。請閱讀此文檔https://www.nginx.com/resources/wiki/start/topics/examples/likeapache-htaccess/ – 2017-01-10 01:17:46

9

還沒有測試過,但外觀好於the one Alex提到。

說明在winginx.com/en/htaccess說:

關於htaccess to nginx converter

的服務是轉換一個Apache的.htaccess nginx的配置說明。

首先,服務被認爲是一個mod_rewrite到nginx轉換器。但是,它允許您將一些其他有理由從Apache移植到nginx的指令。

注意服務器指令(例如php_value等)將被忽略。

轉換器不檢查語法,包括正則表達式和邏輯錯誤。

請在使用前手動檢查結果。

+0

winginx.com/htaccess不起作用 – 2014-02-27 08:22:22

+0

昨天才注意到它現在正在運行,是的。希望它會回到網上。 – 2014-02-27 10:30:50

3

使用此: http://winginx.com/htaccess

在線轉換器,很好的辦法和節省時間;)

+2

如果你仔細觀察,你可以看到我在發佈之前發佈了該鏈接方式。 – 2014-05-05 14:58:54

+0

真男人,對不起! – ErickXavier 2016-08-24 03:50:18