Question: I want to validate url before redirect it using Flask.

My abstract code is here...

```
@app.before_request
def before():
   if request.before_url == "http://127.0.0.0:8000":
       return redirect("http://127.0.0.1:5000")

```

Do you have any idea? 
Thanks in advance.

Answer: 
You can use **urlparse** from **urllib** to parse the url. The function below which checks `scheme`, `netloc` and `path` variables which comes after parsing the url. Supports both Python 2 and 3.

```
try:
    # python 3
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

def url_validator(url):
    try:
        result = urlparse(url)
        return all([result.scheme, result.netloc, result.path])
    except:
        return False

```
You can do something like this (not tested):

```
@app.route('/<path>')
def redirection(path):
    if path == '':    # your condition
        return redirect('redirect URL')

```