user2111433
user2111433

Reputation: 21

Python Flask redirection not working if the url has /#/ in it

I'm new to Python and Flask, i need redirect along with arbitrary or full url, its working fine with the blow code.

but if the url has '/#/' in it then its not working, users are bookmarked these urls and i need to redirect to new domain as part of migration.

Example:

user url: http://127.0.0.1:5000/abc1/cba2/#/yyyy/2001
URL to be redirected: http://168.192.0.12:5000/abc1/cba2/#/yyyy/2001

Its not working since the there is # sign, Flask captures url only till # sign [/abc1/cba2/], because of this the redirection fails

How do i resolve this issue?

from flask import Flask, redirect
import json

def create_app():
    app = Flask(__name__)

    @app.route('/', defaults={'arbitrary': ''}, methods=['GET'])
    @app.route('/<path:arbitrary>')
    def red(arbitrary):
        print (arbitrary)
        url_substring = "/yyyy/"
        if url_substring in arbitrary:
            new_path = 'https://xxxx.com/' + arbitrary
            print (new_path)
            return redirect(new_path, code=302)
        else: 
            return redirect("https://xxxx.com", code=302)

Upvotes: 1

Views: 207

Answers (3)

user2111433
user2111433

Reputation: 21

Fragment Url's [whatever after #] will not be sent to server side, so JS is the only option, I have used NodeJS [Server side] and JS for this.

Once the customer click's the bookmarked URL it will reach to NodeJS from there it will be routed to the HTML page which got the JS to get the fragment URL and redirect accordingly.

The below JS will get the Fragment URL

var urlAddress = document.URL.substr(document.URL.indexOf('#')+1);

Upvotes: 0

avijit
avijit

Reputation: 911

According to http specification, content after # sign can be accessed only by client. In your situation, you can read the omitted part after loading a sample page which sends ajax request with the omitted content after # from client side.

Please see here for reference. https://stackoverflow.com/a/9384407/4374376

Upvotes: 1

Nerveless_child
Nerveless_child

Reputation: 1410

Are you aware of what the # sign means in a URL?

If you must include it then you have to escape the sign.

URL should be: http://127.0.0.1:5000/abc1/cba2/%23/yyyy/2001

%23 represents the # sign.

Upvotes: 0

Related Questions