V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
holinhot
V2EX  ›  NGINX

apache 重写怎么转 nginx

  •  
  •   holinhot · 2015-02-21 02:56:49 +08:00 · 3070 次点击
    这是一个创建于 3358 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RewriteCond %{THE_REQUEST} ^GET\s.+.php [NC]
    RewriteRule ^(.+).php$ /$1 [NE,R=301,L,NC]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]

    在线转换工具不管用啊

    17 条回复    2015-02-22 00:37:39 +08:00
    holinhot
        1
    holinhot  
    OP
       2015-02-21 02:58:41 +08:00
    大神给看看
    zmr90
        2
    zmr90  
       2015-02-21 09:08:41 +08:00
    http://90r.org/628.html 试下里面的网站,我试过还可以的
    extreme
        3
    extreme  
       2015-02-21 10:19:13 +08:00   ❤️ 1
    虽然我第一个使用的HTTP Server是Apache,但我开始自己写rewrite却不是Apache,而是Nginx,因此对mod_rewrite的语法不太了解。
    也许你可以尝试说明用途(预期效果),而不是给mod_rewrite的规则,说不定别人更容易帮到你。
    holinhot
        4
    holinhot  
    OP
       2015-02-21 14:19:09 +08:00
    holinhot
        5
    holinhot  
    OP
       2015-02-21 14:19:37 +08:00
    @louishothot 有利于seo
    holinhot
        6
    holinhot  
    OP
       2015-02-21 14:20:53 +08:00
    @extreme 不修改网站源代码的前提下
    holinhot
        7
    holinhot  
    OP
       2015-02-21 14:31:58 +08:00
    holinhot
        8
    holinhot  
    OP
       2015-02-21 14:33:13 +08:00
    liuhaotian
        9
    liuhaotian  
       2015-02-21 15:45:23 +08:00   ❤️ 1
    if ($request_filename ~ "(.*)\.php") {
    rewrite ^/(.*)\.php$ http://www.hwbj.net/$1 permanent;
    }
    if ($request_filename !~ "(.*)\.php") {
    rewrite ^/(.*)$ /$1.php last;
    }
    extreme
        10
    extreme  
       2015-02-21 15:55:42 +08:00
    @louishothot
    试试这个:
    rewrite ^/(.*)\.php$ /$1 permanent;
    if (!-f $request_filename){
    set $rule_0 1$rule_0;
    }
    if (!-d $request_filename){
    set $rule_0 2$rule_0;
    }
    if ($rule_0 = "21"){
    rewrite ^/.*$ $fastcgi_script_name.php?$query_string last;
    }
    extreme
        11
    extreme  
       2015-02-21 16:17:45 +08:00
    @louishothot 刚刚测试了一下,发现一点问题,这个是完善的版本:
    rewrite "^/(.*)\.php(/){0,}$" /$1 permanent;
    if (!-f $request_filename){
    set $rule_0 1$rule_0;
    }
    if (!-d $request_filename){
    set $rule_0 2$rule_0;
    }
    if ($rule_0 = "21"){
    rewrite ^/.*$ $fastcgi_script_name.php last;
    }
    ryd994
        12
    ryd994  
       2015-02-21 17:43:35 +08:00   ❤️ 1
    @liuhaotian
    @extreme
    诸位,if is evil,各种nginx文档里都已经强调过无数次了
    http://wiki.nginx.org/IfIsEvil


    location ~ (.+)\.php$ {
    return 301 $1;
    }

    location / {
    try_files ${uri}.php =404;
    }
    extreme
        13
    extreme  
       2015-02-21 17:45:49 +08:00
    @ryd994 Amazing!
    liuhaotian
        14
    liuhaotian  
       2015-02-21 17:55:37 +08:00   ❤️ 1
    @ryd994 一直不是很懂try_files怎么用,看来要去好好学学了。谢谢@
    ryd994
        15
    ryd994  
       2015-02-21 18:12:57 +08:00 via Android
    @liuhaotian try_files就是搜索后面的每个文件(除了最后一个参数),如果存在,则uri变成相应的uri,但仍在当前location继续处理。如果文件都不存在,则直接到最后一个参数所在的location,最后一个参数除了普通uri,也可使用=httpcode和@namedblock
    liuhaotian
        16
    liuhaotian  
       2015-02-21 18:21:58 +08:00   ❤️ 1
    @ryd994 这样子 那也就是说try_files可以代替rewrite吗。。
    rewrite /(.*)/(.*)$ index.php?c=$1&b=$2;
    是不是可以写成
    location ~ /(.*)/(.*)${
    try_files index.php?c=$1&b=$2 =404;
    }
    这样子?
    ryd994
        17
    ryd994  
       2015-02-22 00:37:39 +08:00 via Android
    @liuhaotian try_files不带get参数
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2285 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:14 · PVG 11:14 · LAX 20:14 · JFK 23:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.