redirect old php urls to new flask urls redirect old php urls to new flask urls flask flask

redirect old php urls to new flask urls


If using apache, putting rewrite rules into either the directory section of the httpd.conf file or into an .htaccess file would probably be the easiest way to do this.


Yes, it can be done in Flask.

I will show you first the code and then explanation.

Code

    @app.route('/index.php')    def index():        url = url_for('search',q=request.args['q'])        return redirect(url)    @app.route('/search')    def search():        # Access q as request.args['q'] same as we have done above

Explanation

Whenever Flask receives some parameters appended to a valid url, in this case q appended to 'index.php' url, with ? as the joining element. It saves those parameters in a MultiDict (Read about it here) named as args which is available as an attribute of global request variable of Flask.

You access it as

    request.args['parameter_name']

Remember that parameter value has been converted to a string and any integer value should be passed through int().

This code simply catches index.php url and it's parameter and then redirect to your new search url with parameter passed as it is. You can also process parameters and pass on to search url. This way both urls will ultimately do your desired work.

Hope that helps! :)


Flask doesn't care what the names in rules are because they don't point to files. You could write:

@app.route('/index.php')def index_view():    pass