Forum Discussion

dgloff_22332's avatar
dgloff_22332
Icon for Nimbostratus rankNimbostratus
Jun 25, 2012

Truncate an URL

Hello,

 

 

I had an SEO project dropped on my desk that I'm fairly certain can be done via an iRule, but I'm not sure how to handle the truncation. Basically, they're looking to redirect all requests from /anything/default.asp to /anything/. I can easily do this on a 1:1 basis by just doing a straight redirect from /dir1/default.asp to /dir1/. But I'm not sure how to write it so it'll handle /dir2/default.asp when that inevitably gets added.

 

 

I'm sure this is fairly simple, but I'm about 5 iRules deep (and all fairly simple ones at that), so I'm just not sure how. This is the first one where I haven't been able to find an example already existing on DevCentral that I can change to suit my needs.

 

2 Replies

  • After some more scavenging for potential solutions, I came up with this, which seems to work:

     

     

    when HTTP_REQUEST {

     

    if { [string tolower [HTTP::uri]] contains "/default.aspx" } {

     

    set replaceURI [string map [list /default.aspx /] [HTTP::uri]]

     

    HTTP::redirect "$replaceURI"

     

    }

     

    elseif { [string tolower [HTTP::uri]] contains "/default.asp" } {

     

    set replaceURI [string map [list /default.asp /] [HTTP::uri]]

     

    HTTP::redirect "$replaceURI"

     

    }

     

    }

     

     

    Any potential downsides that I may have missed?
  • To complicate things further, after getting the above working, Marketing informed me that they wanted a 301, not a 302. So I've changed it to the following:

     

     

    when HTTP_REQUEST {

     

    if { [string tolower [HTTP::uri]] contains "/default.aspx" } {

     

    set replaceURI [string map [list /default.aspx /] [HTTP::uri]]

     

    HTTP::respond 301 Location "$replaceURI"

     

    }

     

    elseif { [string tolower [HTTP::uri]] contains "/default.asp" } {

     

    set replaceURI [string map [list /default.asp /] [HTTP::uri]]

     

    HTTP::respond 301 Location "$replaceURI"

     

    }

     

    }