how to redirect to particular url on 404

流过昼夜 提交于 2020-01-02 11:38:53

问题


@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?


回答1:


@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.




回答2:


    @app.error(404)
    def error(err):
        bottle.response.status = 303 
        bottle.response.header['Location'] = '/' 



回答3:


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,



来源:https://stackoverflow.com/questions/4286632/how-to-redirect-to-particular-url-on-404

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!