1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Crude way of doing 301 redirects semi-manually

Discussion in 'Tips and Guides [Archive]' started by yoghurtfarmer, Oct 9, 2010.

  1. yoghurtfarmer

    yoghurtfarmer Well-Known Member

    If you're importing from a forum software other than vB, here's a crude way of doing 301 redirects provided that you have a sitemap of all your threads.

    You will need python, which can be downloaded here:

    Open up IDLE (Python GUI). Click File/New Window and copy and paste the scripts.

    1. Extract all of your original thread links from the sitemap. Take note of if your threads are listed chronologically or reverse chronologically.

    This is something I put together in python (with a lot of googling and learning!) to extract the links from XML sitemaps. It worked for me but you may have to modify it a bit to work for you.

    If you do decide to use it, fill in your domain, sitemap location, and path for the saved .txt file under the commented sections.

    #!/usr/bin/env python
    import re
    # your website domain, keep the format
    # path of the sitemap, locally stored
    path = "C:\Downloads\sitemap.xml"
    # path and file name of resulting text file
    path2 = "D:\Wherever\Old Forum URLs.txt"
    fileHandle = open(path, "r")
    sitemap = fileHandle.read()
    replaced = sitemap.replace("<loc>"+domain, "redirect 301 ")
    list = re.findall("redirect 301.*</loc>", replaced)
    fileHandle = open(path2, "w")
    for item in list:
            final = item.replace("</loc>","\n")
    print "Completed. Results have been saved to", path2

    2. After you have all your old thread URLs, you need the new ones. For this, we can take advantage of the fact that in Xenforo, as long as the thread ID is correct you'll get redirected to the correct thread.

    You need to know the total number of threads on your forum.

    You could manually go to ...threads/whatever.1/
    Copy the redirected link and then go to ...threads/whatever.2/
    And so on...

    That works fine or a small board, in fact that's what I did it for 100/467 threads. I only came up with a quicker way to do this during my long break at school (yes, with python).

    Fill in the relevant sections, they're commented in the code. If your sitemap lists threads in reverse chronological order, the code is fine as it is.

    If your sitemap is chronological:

    Replace for thread in reversed(range(1, totalthreads+1)): with for thread in range(1, totalthreads+1):

    #!/usr/bin/env python
    import urllib2
    # path to your xenforo threads directory
    xenpath = ""
    # total number of threads on your forum
    totalthreads = 467
    # path to save the resulting text file
    savepath = "D:/Wherever/New Forum URLs.txt"
    filehandle = open(savepath, "w")
    for thread in reversed(range(1, totalthreads+1)):
        link = xenpath+"a.%s/" % thread
        response = urllib2.urlopen(link)
        url = response.geturl()
    print "Completed. Results have been saved to", savepath

    3. Now you have 2 text files with links that should match line by line. Open up a spreadsheet in excel. Paste the original links into the first column and the new links into the second column. Now copy and paste both columns and paste them into Notepad++ or another editor. Fix the spacing so that it'll work in .htaccess file.

Share This Page