The easiest thing to do is to introduce something like the characteristic function,the Kronecker delta function, or an Iverson bracket, or the heaviside step function. You can find information on those on wikipedia or proofwiki.org. They allow you to assign a variable that takes on $1$ under some condition and $0$ otherwise. Then your function won't be undefined otherwise, it will be zero, but in practical applications that's generally just fine, if not better.
Let's say you don't want to do that, for some reason.
For a restriction to an unbounded interval, start with a function that has an easy to manipulate domain. I'll use $\ln$, but $\sqrt{\phantom{x}}$ works just as well.
$$\ln x : x > 0$$
You can easily switch the direction of this by a minus sign:
$$\ln (-x) : -x > 0$$
and we have $-x > 0 \iff x < 0$
Let's say I want to limit my function to $x < -2$. Then I can multiply by:
$$\frac{\ln (-x-2)}{\ln (-x-2)} = 1: -x - 2 > 0$$
and we have $-x - 2 > 0 \iff x < -2$
You can do this more than once. Let's say I want to limit the function to $-10 < x < -2$. Then we can combine the restrictions of $x > -10$ and $x < -2$:
$$\frac{\ln (-x-2)}{\ln (-x-2)} \frac{\ln (x+10)}{\ln (x+10)} = 1: -x - 2 > 0,x+10 > 0$$
and we have $x < -2 \text{ and } x> -10$.
Offhand, I can't think of a way to exclude the endpoints in the restriction, but if I think of one I'll update this answer.
Edit: AlexR has an excellent way to exclude the endpoints in the restriction.