Forum Discussion

Thiyagu_343098's avatar
Thiyagu_343098
Icon for Nimbostratus rankNimbostratus
May 24, 2018

URL redirect with 301 from old host name to new host name for many URLs

Hello All, I'm working on task for an URL redirection from the old host to the new host for many URLs.

 

for example any URL of *.test.com should get redirected to *.result.com.

 

Regards, Thiyagu

 

8 Replies

  • Surgeon's avatar
    Surgeon
    Ret. Employee

    You can create an iRule to do it for you using http_request event. if you have multiple URLs to redirect you can use switch command. As alternative you can use stream profile to replace all .test.com to .result.com.

     

  • You could use getfield function to grep host name.

    iRule will be some thing like below. Paste the comments if any help require.

        when HTTP_REQUEST {
        if { [HTTP::host] contains "test.com"} {
          HTTP::respond 301 Location "https://[getfield [HTTP::host] ".test.com" 1].result.com[HTTP::uri]
      }
    }
    

    Cheers...

    • Thiyagu_343098's avatar
      Thiyagu_343098
      Icon for Nimbostratus rankNimbostratus

      Hi, I have prepared the below script for an URL redirection based on host name with top level domain value. Can you please advise whether I have made any error or any where I have to change the logic?

       

      when HTTP_REQUEST { if {[string tolower [HTTP::host]] starts_with ".testold1.com"}{ set newurl [string map -nocase {".testold1.com" ""}[HTTP::host] ".testnew.com" 1] HTTP::respond 301 Location "http://.[HTTP::host $ newurl][HTTP::uri] } else if {[string tolower [HTTP::host]] starts_with ".testold.com"}{ set newurl [string map -nocase {".testold.com" ""}[HTTP::host] ".testnew.com" 1] HTTP::respond 301 Location "http://.[HTTP::host $ newurl][HTTP::uri] } }

       

      OR

       

      when HTTP_REQUEST { if { ([HTTP::host] contains ".testold1.com") { HTTP::respond Location 301 "https://[getfield [HTTP::host] ".testold1.com" 1].testnew[HTTP::uri] } else if { ([HTTP::host] contains ".testold2.com") { HTTP::respond Location 301 "https://[getfield [HTTP::host] ".testold2.com" 1].testnew[HTTP::uri] } }

       

    • Thiyagu_343098's avatar
      Thiyagu_343098
      Icon for Nimbostratus rankNimbostratus

      Hello All, I have tried the below script and it is throwing the error while saving the irule. Could you please help me to know anything syntax I have to change with the below script?

       

      when HTTP_REQUEST { if { [HTTP::host] contains "test.com"} { HTTP::respond 301 Location "https://[getfield [HTTP::host] ".test.com" 1].result.com[HTTP::uri] } }

       

      Your quick help would be appreciable.

       

      Regards, Thiyagu

       

    • Samir_Jha_52506's avatar
      Samir_Jha_52506
      Icon for Noctilucent rankNoctilucent

      See below(comma is missing at end)

       when HTTP_REQUEST {
           if { [HTTP::host] contains "test.com"} {
             HTTP::respond 301 Location "https://[getfield [HTTP::host] ".test.com" 1].result.com[HTTP::uri]"
          }
       }