【问题标题】:.htaccess rewrite to nginx config.htaccess 重写到 nginx 配置
【发布时间】:2012-09-13 15:17:47
【问题描述】:

在将 .htaccess 重写规则转换为 nginx 配置时需要帮助。规则来自 Metro shrink url Shortner 脚本。

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]

【问题讨论】:

    标签: url redirect nginx rewrite


    【解决方案1】:

    以下应该可以解决问题:

    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 标志本质上是默认值,来自 http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

     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
    

    【讨论】:

    • 感谢您的回复,但我遇到了两个错误 1)无效参数“=404” 2)“set”指令中的参数数量无效
    • 另一个错误 [emerg] pcre_compile() failed missing ) in "^([0-9a-zA-Z-]"
    • pcre_compile 是因为正则表达式涉及花括号,也用于描述块,因此必须引用正则表达式。固定在我的答案中
    • 无效参数 =404 已修复,set 指令也是如此(=404 是由于我在决定使用重写之前留下的一部分 try_files 导致的更清晰,我忘记设置的那个添加第二个参数,告诉它设置 $id 为),两者都是固定的
    【解决方案2】:

    试试:

    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; 
    }
    

    【讨论】:

    • 感谢在我将所有“break”更改为“last”后它工作正常
    猜你喜欢
    • 2017-07-25
    • 2014-08-09
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2013-11-29
    • 2016-05-09
    • 1970-01-01
    • 2020-01-10
    相关资源
    最近更新 更多