• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Script to generate 301 redirects

dbembibre

Active member
#1
I dont know if this can be usefull for anyone, in any case, i prefer to share it.
I do my migration manually, with raw sql queries, i dont have import log table and i cant use the Kier redirection scripts, i came from vB3.8 with vBSEO presetting 002.

My original vBSEO URLs look like this:
f7=forum id;
usar-un-polimetro-tester-finalizado = post title
409892 = thread id.
www.mysite.com/f7/usar-un-polimetro-tester-finalizado-409892/
the XenForo URL have the following format:
www.mysite.com/threads/usar-un-polimetro-tester-finalizado.409892/

I sure that this can be achieved with a RewriteRule, but i can found a way to do it well.
By this motive i make a little app to translate old URLs into new URL format with Mono.

In google webmaster tools internal links option of traffic section you can download a excel (csv or google docs) with the 1000 Urls more linked to your site, see attached, sorry i have wemaster tools in Spanish :D

Captura de pantalla 2013-06-21 a la(s) 10.41.04.png

Choose google docs, and download it. open with excel, copy the first column to a text file and save it.

I do this little application in Mono C# on a Mac, Mono work on linux windows and Mac.
Click i new C# project, console type and copy and paste the following code in Program.cs.
You need to change the following entries in the code with your params.
Put you previously saved file with google URLs
string[] sArray = System.IO.File.ReadAllLines(@"/Users/Danny/tmp/urls.txt");
Output file with all your redirects
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"/Users/Danny/tmp/urls_output.txt", true))
Choose RedirectPermanent, Redirect 301, or all that you want.
file.WriteLine("RedirectPermanent " + sArray + " http://www.mysite.com/threads" + sAux);

PHP:
using System;
namespace Generator
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //file that contain all the google urls
            string[] sArray = System.IO.File.ReadAllLines(@"/Users/Danny/tmp/urls.txt");

            string sTemp=string.Empty; string sAux=string.Empty;
            //System.Text.StringBuilder sBuild = new System.Text.StringBuilder ();
            for (int i=0; i< sArray.Length; i++ )
            {
                try {
                    sAux = string.Empty;
                    sTemp = sArray [i]; sAux = sArray [i];
                    //replace - by .
                    sTemp = sTemp.Substring (0, sTemp.LastIndexOf ("-"));
                    sTemp += "." + sAux.Substring (sTemp.Length +1, sAux.LastIndexOf ("/") - sTemp.Length);
                    sAux = string.Empty;
                    //forumid to replace
                    int iTemp = 0;
                    iTemp = sTemp.Substring (1).IndexOf ("/") + 1;
                    sAux = sTemp.Substring (iTemp);
                    //sBuild.Append ("RedirectPermanent " + sArray[i] + " http://www.mysite.com/threads" + sAux + "\n");
                    //file to write the 301 redirects
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"/Users/Danny/tmp/urls_output.txt", true))
                    {
                        file.WriteLine("RedirectPermanent " + sArray[i] + " http://www.misite.com/threads" + sAux);
                    }
                } catch (Exception ex) {
                    System.Diagnostics.Debug.Print (ex.Message);
                    continue;
                }
            }
            //System.Diagnostics.Debug.Print (sBuild.ToString());
            //sBuild.ToString();
        }
    }
}
 

Jake Bunce

XenForo moderator
Staff member
#2
Put this in your .htaccess file in the web root:

Code:
RewriteRule ^[^/]+/.+-([0-9]+)/ /threads/$1/? [R=301,L]
It should go immediately after:

Code:
RewriteEngine On