0

I am trying to redirect the URL based on a pattern in Go. If my URL is containing "clientApi" then I am sending it to clientApiPoint func otherwise I am sending it to redirectApiPoint func. My handleRequest func is

func handleRequest()  {
    r := mux.NewRouter()
    r.HandleFunc("/", homePage)
    r.HandleFunc("/clientApi", clientApiPoint)
    r.HandleFunc("/{^((?!clientApi).)*$}", redirectApiPoint)
    http.Handle("/", r)
    log.Fatal(http.ListenAndServe(":8081", nil))
} 

{^((?!clientApi).)*$} regex is working fine if my url is something like

localhost:8081/somerandonurl  (sending it to redirectApiPoint func)

but if there one or more "/" in the url, regex is not redirecting it to redirectApiPoint func.

localhost:8081/somerandonurl/somethingdifferent    (not sending it to redirectApiPoint. 404 page not found message)
2
  • 1
    http.HandleFunc() can not be used to register a pattern to match a regular expression. Commented May 6, 2019 at 7:01
  • Are you taking the / into consideration? Also, you might not want to compile a regular expression every time the endpoint is hit - it's an expensive operation. Compile it elsewhere just once(if it won't change). Commented May 6, 2019 at 7:04

1 Answer 1

1

your Regex work on "localhost:8081/somerandonurl" because of the first "/" so it matched.

you need somthing like "/(.*$)" but this will match any /blala/blala/...

you can test your regex from here: Regex

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.