V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Giethoorn
V2EX  ›  问与答

js 能实现全站 301 重定向吗?

  •  
  •   Giethoorn · Oct 14, 2019 · 4986 views
    This topic created in 2398 days ago, the information mentioned may be changed or developed.
    上次问 http 跳转到 https 的问题,得到很多站友的回复,原谅我确实有些笨,最后只能采用最简单的 JS 跳转,就是在模板文件中添加 Js
    <script type="text/javascript">
    var url = window.location.href;
    if (url.indexOf("https") < 0) {
    url = url.replace("http:", "https:");
    window.location.replace(url);
    }
    </script>
    感谢 xiaodongus 提供的这段 JS 代码,这个添加后,确实能跳转了,但是我通过在线检测,发现带 http 的链接返回码还是 200,而非 301,JS 能不能实现 301 的全站定向呢?关于质疑我做广告的,就请绕道而行吧!
    30 replies    2021-06-28 11:01:14 +08:00
    optional
        1
    optional  
       Oct 14, 2019
    在线检测 并不会执行 js。
    optional
        2
    optional  
       Oct 14, 2019
    301 是服务端的事情,js 跳转是客户端执行的
    Giethoorn
        3
    Giethoorn  
    OP
       Oct 14, 2019
    .htaccess 配置如下
    <IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
    </IfModule>
    Giethoorn
        4
    Giethoorn  
    OP
       Oct 14, 2019
    @optional 您要非要较真儿,我就添加上去吧,因为之前添加了之后,返回码不对,所以删掉了
    westoy
        5
    westoy  
       Oct 14, 2019
    问题是没意义啊

    全站 HTTPS 就是了防劫持

    你先 HTTP 再跳转 HTTPS 本身就会被劫持搞降级攻击
    santom
        6
    santom  
       Oct 14, 2019
    如果你一定要说 js 的话 服务器跑个 node 然后 redirect?
    Giethoorn
        7
    Giethoorn  
    OP
       Oct 14, 2019
    @westoy 那不能让 http 的打不开吧,还是不开 https 的,我现在都蒙了,到底要不要开 https 的,
    qinxi
        8
    qinxi  
       Oct 14, 2019
    Hsts
    mercury233
        9
    mercury233  
       Oct 14, 2019
    @westoy 检查嵌套 iframe,加 hsts,对于网页来说基本能解决劫持问题的吧

    @Giethoorn 这不是较真,客户端 js 是不可能给你真正的 301 的
    momocraft
        10
    momocraft  
       Oct 14, 2019
    serviceworker 可以假造 301 嗎?
    viko16
        11
    viko16  
       Oct 14, 2019
    没必要自己搞 301,配置 HSTS 和加入 HSTS preload list 就好了
    locoz
        12
    locoz  
       Oct 14, 2019 via Android
    js 不能实现,但是你可以在反代层实现( CDN 提供的全站 HTTPS 就是这么实现的
    shansing
        13
    shansing  
       Oct 14, 2019
    JS 做不了。301 是 HTTP 状态码,JS 改变不了。

    @viko16 加入 Preload List 的前提就是需要“redirect”,这里说的重定向肯定不是 JS 这种。
    wysnylc
        14
    wysnylc  
       Oct 14, 2019
    http->https 有可能被中间人劫持
    EridanusSora
        15
    EridanusSora  
       Oct 14, 2019 via Android
    js 可以啊 nodejs
    shansing
        16
    shansing  
       Oct 14, 2019
    @Giethoorn 看到你在用 .htaccess,那么再加入以下代码就能实现 HTTP 301 重定向到 HTTPS 了:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    MaxSupermist
        17
    MaxSupermist  
       Oct 14, 2019
    301 需要服务端输出 Header 信息,你需要的更可能是 301 全站转发,看这个
    301chongdingxiang.mfpad.com
    Giethoorn
        18
    Giethoorn  
    OP
       Oct 14, 2019
    @shansing 怎么加入,我刚才把这一行代码加进去,RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]然后全站就不能访问了,搞的我都不敢试了
    irainsoft
        19
    irainsoft  
       Oct 14, 2019
    不会是在拿生产环境在做测试吧... 本地搞个环境出来解决问题吧,否则畏手畏脚的做不出来啥的
    beastk
        20
    beastk  
       Oct 14, 2019 via iPhone
    js 一般是 200,服务端才能 301
    tomczhen
        21
    tomczhen  
       Oct 14, 2019 via Android
    明明花点钱昨天说不定就搞完了,非要发两个帖子,折腾好几次,还在那里折腾。
    b821025551b
        22
    b821025551b  
       Oct 14, 2019
    让老板花点钱请个专业的,你基本原理都不懂,这么在线上环境折腾 2 天谁能受得了。
    shansing
        23
    shansing  
       Oct 14, 2019
    @Giethoorn 你为什么只加一行呢……
    Giethoorn
        24
    Giethoorn  
    OP
       Oct 14, 2019
    @tomczhen 在哪找人,我自己花钱都弄好,唉,公司没有技术,网建服务差的要死,我能怎么办,我也很绝望啊
    treblex
        25
    treblex  
       Oct 14, 2019
    服务端开强制 https 不行吗
    just1
        26
    just1  
       Oct 14, 2019
    1,2 楼给了你正确回答你偏说人家较真,那大家真的不知道怎么办了
    Giethoorn
        27
    Giethoorn  
    OP
       Oct 15, 2019
    @suke971219 可以,现在的情况是带 http 的,和 https 的都能访问,需要做 http 的 301 定向到 https 上去
    shangyes
        28
    shangyes  
       Oct 15, 2019
    HSTS 强制 https 访问,或者服务端 Nginx 或者 Apache 设置 rewrite
    Giethoorn
        29
    Giethoorn  
    OP
       Oct 15, 2019
    @shangyes 有没空,接个私单
    xzsljc
        30
    xzsljc  
       Jun 28, 2021
    可以 am
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2751 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 76ms · UTC 15:59 · PVG 23:59 · LAX 08:59 · JFK 11:59
    ♥ Do have faith in what you're doing.