Getting variable of requesting host Apache htaccess?

0

Issue

I have the below code, that I can use the below to set a variable:

SetEnvIf Host (.*) custom_host=$1

What I want is a variable of the requesting host/computer.

For example, if the website http://example.com:32/welcome is making the request, I want this as the variable?

Solution

Instead of Host you need Referer to access the HTTP Referer header that is sent in the request. For example:

SetEnvIf Referer (.*) custom_referer=$1

However, depending on the server-side scripting language you are using, you rarely need to assign this to another variable. You can usually reference it directly as required.

Also note, the HTTP Referer header might not be set. The referring site and the user’s browser can both block this.

UPDATE:

how can i have just the protocol, domain and host without the path.

Try changing the regex to something like:

SetEnvIf Referer ^(https?://[^/]+) custom_referer=$1

If the Referer is http://example.com:32/welcome, then this should result in just http://example.com:32 being stored in the custom_referer variable.

Answered By – MrWhite

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More