404 redirect or disconnect

I have a custom site where valid users will always login and have a
valid session (and a cookie or headers to represent it).

I would like to do the following, but not sure how to start…can
someone just point me in the right direction?

IF user has a valid session (I need to check if the header/cookie is in
the http request), and a 404 is appropriate, re-direct to a specific
location (or a “real” 404 page).

ELSE (the user does not have a valid session for my
website/application), return 444.

Really, I need to understand how to do the if/then and detect the cookie
or header to indicate that the user is “valid”.

Thanks in advance,
AJ