V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
alanying
V2EX  ›  问与答

这样的网址跳转应该如何实现? 送金币

  •  2
     
  •   alanying · 2019-08-05 22:02:36 +08:00 · 1880 次点击
    这是一个创建于 1719 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的地址是 :abc.com/help1.html

    我想输入地址后,自动跳转到 edf.com/help1.html?aff=001

    然后自动在后门加上 aff 参数,这样的网址跳转应该如何实现?

    20 条回复    2019-08-06 13:23:51 +08:00
    alanying
        1
    alanying  
    OP
       2019-08-05 23:06:44 +08:00
    顶起求看到
    Humorce
        2
    Humorce  
       2019-08-05 23:32:19 +08:00   ❤️ 1
    Rewrite
    registerrr
        3
    registerrr  
       2019-08-05 23:35:40 +08:00 via Android
    直接 onload (){window.open(window.location.href + “?aff=001 “)}
    luofeii
        4
    luofeii  
       2019-08-05 23:45:34 +08:00
    后端 setHeader("Location",跳转地址拼接参数),状态码 307
    alanying
        5
    alanying  
    OP
       2019-08-06 00:16:29 +08:00
    @Humorce 就是不太想的好 Rewrite 该怎么写
    alanying
        6
    alanying  
    OP
       2019-08-06 00:16:53 +08:00
    @registerrr 在 nginx 那边加这个么?
    limuyan44
        7
    limuyan44  
       2019-08-06 01:37:32 +08:00   ❤️ 1
    server {
    listen 80;
    server_name abc.com;
    access_log logs/host.access.log main;
    if ( $request_uri = "/help1.html" ){
    rewrite ^/(.*)$ http://www.edf.com/$1?aff=001 permanent;
    }
    ysc3839
        8
    ysc3839  
       2019-08-06 01:57:06 +08:00 via Android
    @limuyan44 这种情况用 return 301 不行吗?一定要用 rewrite 吗?
    ysc3839
        9
    ysc3839  
       2019-08-06 02:03:40 +08:00 via Android   ❤️ 1
    @limuyan44
    Nginx 官方文章 Pitfalls and Common Mistakes 恰好就写了这种错误
    https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#taxing-rewrites

    可以改成 location block + return 301 的形式。
    limuyan44
        10
    limuyan44  
       2019-08-06 02:08:08 +08:00
    @ysc3839 都可以
    limuyan44
        11
    limuyan44  
       2019-08-06 02:24:59 +08:00   ❤️ 1
    @ysc3839 看了下文章,return 确实要好,
    @alanying lz 用这个吧
    if ( $request_uri = "/help1.html" ){
    return 301 http://www.edf.com/help1.html?aff=001;
    }
    registerrr
        12
    registerrr  
       2019-08-06 07:55:04 +08:00 via Android
    @alanying 我这个是纯前端的,能在后端实现更好
    opengps
        13
    opengps  
       2019-08-06 08:58:29 +08:00 via Android   ❤️ 1
    前端后端控制都行,建议用后端的 redirect
    alanying
        14
    alanying  
    OP
       2019-08-06 10:07:22 +08:00
    @limuyan44 如果要 help1.html 要修改成其他的页面呢?
    limuyan44
        15
    limuyan44  
       2019-08-06 10:49:31 +08:00 via Android
    不是很明白你说的其他页面什么意思,按你的要求地址都是写死的,你把 help.html 换成其他页面就好了啊。
    alanying
        16
    alanying  
    OP
       2019-08-06 12:13:45 +08:00
    @limuyan44 也可以用通配符替换页面名称么? ^(( https|http|ftp|rtsp|mms)?:\/\/)[^\s]+
    limuyan44
        17
    limuyan44  
       2019-08-06 12:32:43 +08:00   ❤️ 1
    @alanying 支持的,但是还是没明白你要干什么。可能你要的是下面这种?转发请求到新地址并且添加参数?你把 location 后面的表达式换掉,www.zycat.top 换成你自己的就可以了。
    location ~* \.(gif|jpg|jpeg)$ {

    return 301 http://www.zycat.top$uri?aff=001;
    }
    alanying
        18
    alanying  
    OP
       2019-08-06 13:03:47 +08:00
    limuyan44
        19
    limuyan44  
       2019-08-06 13:18:38 +08:00
    @alanying 你这种需求不需要写正则了
    location / {
    return 301 http://www.zycat.top$uri?aff=001;
    }
    alanying
        20
    alanying  
    OP
       2019-08-06 13:23:51 +08:00
    @limuyan44 我自己写了个

    ```bash
    if ($host = 'abc.com') {
    rewrite ^/(.*)$ http://def.com/$1?aff=001 permanent;
    }

    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2425 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:00 · PVG 00:00 · LAX 09:00 · JFK 12:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.