Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Clear all filters
Answers

Url redirect to a subdomain from sepcific URi to subdomain with the same URi

Hello,

We need to achieve this :

www.domain.com/mobile/news.asp (or whatever url after the “mobile”) redirect/rewrite it to : mobile.newdomain.com/news.asp (or whatever url that was entered after the “mobile” in the first place).

Our irule looks like this currently:

when HTTP_REQUEST { if {([string tolower [HTTP::host]] eq "mobile.domain.com")} { HTTP::respond 301 Location "https://mobile.newdomain.com[HTTP::uri]"
} if {([string tolower [HTTP::uri]] contains "/mobile")} {HTTP::respond 301 Location "https://mobile.newdomain.com";} }

What happens it when going to : “www.domain.com/mobile” it redirects to “mobile.newdomain.com” as it should. But when entering “www.domain.com/mobile” it redirects to “mobile.newdomain.com/mobile/news.asp” Any help would be much appreciated! thank you!

0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Try this rule instead (untested for syntax errors)?

when HTTP_REQUEST {

    if { [HTTP::host] eq "mobile.domain.com" } { 
        HTTP::respond 301 Location "https://mobile.newdomain.com[HTTP::uri]"
    } elseif { [string tolower [HTTP::uri]] contains "/mobile" } { 
        HTTP::respond 301 Location "https://mobile.newdomain.com[string map -nocase { "/mobile" "" } [HTTP::uri] ]";
    }

}
0
Comments on this Answer
Comment made 08-Nov-2017 by Chenco 62

Thank you very much you helped me a lot !!

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Hi!

Using the pre-formatted option in the forum makes the iRule readable:

when HTTP_REQUEST {

    set 

    if { [HTTP::host] eq "mobile.domain.com" } { 
        HTTP::respond 301 Location "https://mobile.newdomain.com[HTTP::uri]"
    }

    if { [string tolower [HTTP::uri]] contains "/mobile" } { 
        HTTP::respond 301 Location "https://mobile.newdomain.com";
    }

}

You state in your question that:

Looks like the example needs to be updated?

Another tip is to exampine Chrome developer tools to see where the redirect came from (check the server header). Sometimes requests reaches the application.

Final tip, don't set up if conditions with a result of a redirect where both can be matched. It could lead to unpredictable results and/or error messages in your logs. Better to use if/elseif. :)

/Patrik

0