如何重定向到 404 上的特定网址

发布于 2024-10-04 04:57:41 字数 223 浏览 0 评论 0原文

@error(404)
def error404(error):
    return 'Nothing here, sorry'

这就是bottle框架中响应404的方式。但在 404 上,我想重定向到特定的网址,例如 http://abc.com/。是否可以?

@error(404)
def error404(error):
    return 'Nothing here, sorry'

This is the way to response 404 in bottle framework. But On 404 I want to redirect to particular url say http://abc.com/. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请爱~陌生人 2024-10-11 04:57:42
import urllib
url = urllib.urlopen('www.google.com/testing') #a 404 address
if url.code == 404:
    url = urllib.urlopen('www.google.com')

当创建 urlobject 时,.code 实例返回页面的代码,

import urllib
url = urllib.urlopen('www.google.com/testing') #a 404 address
if url.code == 404:
    url = urllib.urlopen('www.google.com')

When a urlobject is created the .code instance returns the code of the page,

扶醉桌前 2024-10-11 04:57:41
@error(404)
def error404(error):
    from bottle import redirect
    # maybe test the error to see where you want to go next?
    redirect(new_url, 303) # HTTP 303 should be used in this case

编辑我不是100%确定这可以完成,我现在无法测试它,但我稍后会测试它并更新答案,除非你比我先一步。

@error(404)
def error404(error):
    from bottle import redirect
    # maybe test the error to see where you want to go next?
    redirect(new_url, 303) # HTTP 303 should be used in this case

EDIT I'm not 100% sure this can be done, and I can't test it right now, but I'll test it later and update the answer unless you beat me to it.

七色彩虹 2024-10-11 04:57:41
    @app.error(404)
    def error(err):
        bottle.response.status = 303 
        bottle.response.header['Location'] = '/' 
    @app.error(404)
    def error(err):
        bottle.response.status = 303 
        bottle.response.header['Location'] = '/' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文