vADC Forum

Reply
N/A
Posts: 1
Registered: ‎11-29-2012
Accepted Solution

Reading URLs from a file to perform multiple 301 redirects

We have moved website recently and some of the URL structures are quite different. In order to get around this and maintain our SEO rankings we would like to stick a LOT of 301 redirects on our Stingray in order to send people to the right pages.

 

I would usually just do this in traffic script, but there are a lot of URLs here and it's getting difficult to mange.

 

Could someone let me know if / how we could add these URLs to a text file and get the Stingray rule to just use / process stuff in the file?

 

I think the correct function is resource.getxxxx() and the Traffic Sript manual gives the following example of how to call a file:

 

*# Read the contents of the 'info' file and add them # as a new header.
http.addheader( "X-Info", resource.get( "info" )); *

 

However - all this tells me is how to get a file - how on earth do I use it in a rule?

And how might I format the URLs in the text file? Each line of the file would have the old URL and the new URL (to forward to).

 

Any help would be appreciated!!! 

 

JD


 

Occasional Contributor
Posts: 9
Registered: ‎11-29-2012

Re: Reading URLs from a file to perform multiple 301 redirects

Hi,

 

Reading a file in TS is quite easy :

    1. upload your file in the extra catalog

    2. read each line of your file with a resource.getLines() call and a simple loop

 

Here is a simple peace of code that could do the job :

 

##############################################################

$configFile = "yourfile.conf";
$lineNum = 0;
$file = "";

if (resource.exists($configFile)) {
    $file = resource.getLines($configFile);
} else {
    log.info("Unable to get extra resource file : ". $configFile);
    return;
}

 

foreach ( $line in $file ) {
    $lineNum++;

    $line = string.regexSub($line, "*", ""); # remove heading whitespaces<br>    if ( $line == "") continue; # skip blank lines<br>    if ( string.startswith($line, "#") ) continue; # skip comments<br>    if ( string.regexMatch($line, "()(*)$" ) ) {
        $url = string.trim($1);
        $redirect = string.trim($2);
        if ( http.getPath() == $url ) {

            # JUST PUT YOUR REDIRECT CODE HERE Smiley Wink

        }
    } else log.info($configFile." inconsistent line ".$lineNum);
}
##############################################################

 

I have used some regex that are not very effective in terms of performance but they allow your config file to include comments and as many whitespace as you want.

 

To improove efficiency, I would advise you to store file content in a hash table structure and to save it with a persistent data.set.

I will need (a bit) more work, but I think that makes sense. (feel free to ask for more help about that if you need it).

 

I hope this will help you.

 

Regards,

 

Yannick

Frequent Contributor
Posts: 321
Registered: ‎11-29-2012

Re: Reading URLs from a file to perform multiple 301 redirects

This article (Managing the growth of website content) describes how you can achieve exactly this - hope that it helps!

Best regards

Owen

Join the Community

Get quick and easy access to valuable resource designed to help you manage your Brocade Network.