1

I have this website for instance:

I still host that old site but I moved it here:

I'd like each old page to point to each new page. A permanent redirect like this:

https://www.neuraxle.neuraxio.com/* ==> https://www.neuraxio.com/en/neuraxle/*

Do I need to host a server for this to manually writing a Python Flask app that redirects each URL with a custom rewrite? I'd like to avoid coding this by using DNS. Is there a DNS trick I can use? I use GoDaddy to manage my DNS.

Optional: if there is nothing to be done with DNS, can you provide an example Flask URL handler method? Is there a free hosting service available for me to host this flask app?

Guillaume Chevalier
  • 9,613
  • 8
  • 51
  • 79

3 Answers3

1

You should be able to redirect, using RewriteEngine and .htaccess file you should have in your web root or accessible via your hosting control panel, using regular expressions. I got a little unsure about direction of move due to your wording, so just in case, here are both directions with some references.

If you want to redirect from subdomain to folder (which seems more likely as per your example) you can do something like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.neuraxle\.neuraxio.\com$
RewriteCond %{REQUEST_URI} !^/en/neuraxle
RewriteRule ^(.*)$ /en/neuraxle/$1 [L,NC]

If you want to redirect from folder to subdomain, you can do something like:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.neuraxio.com
RewriteRule ^en/neuraxle/(.*)$ http://www.neuraxle.sample.com/$1 [L,R=301]

Some additional references:

Edit: on another thought it depends on deployment option/platform you have used which you haven't mentioned in your question.

And actually, on yet another thought, GoDaddy indeed allows a DNS redirect. Go to Domain manager -> DNS -> DNS Management And near the bottom of the page on the right there will be forwarding section: enter image description here

In the SUBDOMAIN enter: www.neuraxle and in the FORWARD TO select https:// and enter www.neuraxio.com/en/neuraxle/ and this should do the trick. I'll keep mod_rewrite info just in case.

isp-zax
  • 3,833
  • 13
  • 21
  • Is that a PHP server with cPanel? What kind of server is that precisely? – Guillaume Chevalier Oct 23 '19 at 07:48
  • `mod_rewrite` is available in web servers (`Apache` and `nginx` are most common) and if you are using a shared hosting the access to their configuration might be via an `.htaccess` file in the `public_html` or similar named web root folder or, via control panel of the hosting (of which `cPanel` is the most common one). If you are NOT using ANY web server but serving from flask directly, `mod_rewrite` might still be an option https://flask.palletsprojects.com/en/1.1.x/deploying/cgi/ – isp-zax Oct 25 '19 at 01:50
  • Your DNS solution doesn't work and seem to have broken the site. Try to visit https://www.neuraxle.neuraxio.com/ for instance. – Guillaume Chevalier Oct 27 '19 at 22:43
  • If by "have broken the site" you meant the browser message about connection not being private, you have to fix you SSL certificate to cover all of the subdomains - a wildcard certificate as described here, for example: https://ca.godaddy.com/help/what-is-a-wildcard-ssl-certificate-567 Otherwise, the site works fine for me when I bypass the warning about not private connection – isp-zax Oct 27 '19 at 23:11
  • Would there be a way to avoid buying wildcard certificates all along? – Guillaume Chevalier Oct 28 '19 at 00:13
  • To avoid browser security alerts, you either have to buy a wildcard or a multidomain certificate or two certificates: https://serverfault.com/questions/566426/does-each-subdomain-need-its-own-ssl-certificate, alternatively you can stop using SSL altogether and switch to HTTP, lastly you can find a free option (I've used StartSSL before): https://geekflare.com/free-ssl-tls-certificate/ – isp-zax Oct 28 '19 at 05:15
1

This should be of help out.

Sometimes you need to write a website that simply redirects from one domain to another. For example, you might have created a website at yourusername.pythonanywhere.com, and it's become popular enough that you decided to upgrade to a paid plan and host it on www.yourdomain.com -- but there are lots of links out there that go to the old domain, and you want them to continue working.

The best solution is to have a simple website running at yourusername.pythonanywhere.com which, when it receives a request, redirects the user to the new domain -- so requests to http://yourusername.pythonanywhere.com/something are redirected automatically to http://www.yourdomain.com/something, and so on.

To do this, you need to have a website on the "Web" tab for both domains; the one on www.yourdomain.com is your main site, and the one on yourusername.pythonanywhere.com is a simple Flask app using Python 3.6, with the following code in the flask_app.py file:

from flask import Flask, redirect, request

app = Flask(__name__)

from urllib.parse import urlparse, urlunparse

FROM_DOMAIN = "yourusername.pythonanywhere.com"
TO_DOMAIN = "www.yourdomain.com"

@app.before_request
def redirect_to_new_domain():
    urlparts = urlparse(request.url)
    if urlparts.netloc == FROM_DOMAIN:
        urlparts_list = list(urlparts)
        urlparts_list[1] = TO_DOMAIN
        return redirect(urlunparse(urlparts_list), code=301)

OR TRY THE BELOW METHOD

How to redirect to an external domain in Flask?

darligee
  • 69
  • 5
1

This worked, accepted bountied answer is poor:

@app.before_request
def redirect_to_new_domain():
    FROM_DOMAINS = ['0.0.0.0:' + str(PORT), 'https://www.neuraxio.com', 'www.neuraxio.com', "neuraxio.com"]
    SUBPATH = "/en/neuraxle"
    TO_DOMAIN = 'www.neuraxle.org'
    urlparts = urlparse(request.url)
    print(urlparts, urlparts.netloc)
    if urlparts.netloc in FROM_DOMAINS and SUBPATH in urlparts.path:
        parsed = urlparts
        parsed = parsed._replace(netloc=TO_DOMAIN)
        parsed = parsed._replace(path=urlparts.path.replace(SUBPATH, ""))
        return redirect(parsed.geturl(), code=301)

Note: my own answer here doesn't completely answer the original question but at least compiles. We also needed this as I answer now, to the exception that we we also have a sub-path to redirect from instead of to.

Guillaume Chevalier
  • 9,613
  • 8
  • 51
  • 79