2012-09-13 148 views
0

需要幫助將.htaccess重寫規則轉換爲nginx配置。規則來自metro縮小網址縮寫腳本。.htaccess重寫爲nginx配置

RewriteEngine On 
#RewriteBase/

RewriteRule ^developer.html$   developer.php [QSA,L] 
RewriteRule ^multishrink.html$   multishrink.php [QSA,L] 
RewriteRule ^stats.html$    public-stats.php [QSA,L] 
RewriteRule ^feed.rss     feed.php [QSA,L] 
RewriteRule ^([^/]+)\.qrcode$   qrcode.php?id=$1 [QSA,L] 
RewriteRule ^api.php$     API/simple.php [QSA,L] 
RewriteRule ^API/write/(get|post)$  API/write.php?method=$1 [QSA,L] 
RewriteRule ^API/read/(get|post)$  API/read.php?method=$1 [QSA,L] 
RewriteRule ^([^/]+)/stats$    stats.php?id=$1 [QSA,L] 
RewriteRule ^([^/]+)/unlock$   unlock.php?id=$1 [QSA,L] 

# If path is not a directory or file then apply RewriteRule 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d   
RewriteRule ^([0-9a-zA-Z-]{1,60})$  go.php?id=$1 [QSA,L] 

回答

0

以下應該做的伎倆:

location = /developer.html { rewrite^developer.php last; } 
location = /multishrink.html { rewrite^multishrink.php last; } 
location = /stats.html { rewrite^stats.php last; } 

location ~ ^feed.rss { rewrite^feed.php last; } 
location ~ ^([^/]+)\.qrcode$ { rewrite ^([^/]+)\.qrcode$ qrcode.php?id=$1 last; } 

location = /api.php { rewrite^API/simple.php; } 
location ~ ^API/(write|read)/(get|post)$ { 
    rewrite ^API/(write|read)/(get|post)$ API/$1.php?method=$2 last;  
} 

location ~ ^([^/]+)/stats$ { rewrite ^([^/]+)/stats$ stats.php?id=$id last;} 

location ~ ^([^/]+)/unlock$ { 
    set $id $1; 
    rewrite^unlock.php?id=$id last; 
} 

location ~ "^([0-9a-zA-Z-]{1,60})$" { 
    try_files $uri $uri/ go.php?id=$1; 
} 

注意:您可以先救他們再利用從位置的正則表達式的捕獲(如在解鎖位置),或者您可以重複正則表達式(需要保存來自重寫指令重置捕獲變量的事實)。使用你喜歡的任何風格。

注:Nginx是由Apache的默認設置,以便QSA標誌追加查詢參數基本上是默認的,從:

If a replacement string includes the new request arguments, the previous request 
arguments are appended after them. If this is undesired, putting a question mark 
at the end of a replacement string avoids having them appended 
+0

感謝回覆,但我得到了兩個錯誤1)無效參數「= 404」2)在「設置」指令 – user1624300

+0

另一個錯誤[emerg] pcre_compile()失敗失蹤)中的參數無效「^([0-9a-zA- Z-]「 – user1624300

+0

pcre_compile是因爲正則表達式涉及的大括號也用於劃分塊,所以正則表達式必須被引用。在我的回答 – cobaco

0

嘗試:

location = /developer.html { 
    rewrite ^(.*)$ /developer.php break; 
} 

location = /multishrink.html { 
    rewrite ^(.*)$ /multishrink.php break; 
} 

location = /stats.html { 
    rewrite ^(.*)$ /public-stats.php break; 
} 

location /feed { 
    rewrite ^/feed.rss /feed.php break; 
} 

location/{ 
    rewrite ^/([^/]+)\.qrcode$ /qrcode.php?id=$1 break; 
    rewrite ^/([^/]+)/stats$ /stats.php?id=$1 break; 
    rewrite ^/([^/]+)/unlock$ /unlock.php?id=$1 break; 
    if (!-e $request_filename){ 
     rewrite "^/([0-9a-zA-Z-]{1,60})$" /go.php?id=$1 break; 
    } 
} 

location = /api.php { 
    rewrite ^(.*)$ /API/simple.php break; 
} 

location /API { 
    rewrite ^/API/write/(get|post)$ /API/write.php?method=$1 break; 
    rewrite ^/API/read/(get|post)$ /API/read.php?method=$1 break; 
} 
+1

感謝得到它的工作後,我改變所有的「破發」到「最後」 – user1624300