Forum Moderators: phranque

Message Too Old, No Replies

HTTP REFERER as get method

HTTP_REFERER as get method

         

Mahabub

8:51 am on Oct 20, 2010 (gmt 0)

10+ Year Member



Dear All,

is there any way to get HTTP_REFERER as a get variable from .htaccess. i am trying the below one but not work.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} (.*?)
RewriteRule ^play/([^/]+)$ [domain.com...]


url is: domain.com/play/index.php?con=2

i am trying to redirect

domain.com/index.php?con=2&ref=some.site

the above code out put is

domain.com/index.php&ref=somesite?con=2

i missed some thing but i cant figured out it. Thank you forlooking into it.

Thanks
Mahabub

jdMorgan

1:29 pm on Oct 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the [QSA] flag here, to append the "ref=" name/value pair to any existing query string:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_REFERER} ^(.*)$
RewriteRule ^play/([^/]+)$ https://domain.com/$1?ref=%1 [QSA,L]

Also as shown, you should always use an [L] flag on every rule, unless you have a very-specific reason not to do so... and such cases are quite rare.

Be aware that the HTTP Referer header is easily spoofed and that many "media players" will not send one at all. In addition, caching proxies in corporate and ISP networks may effectively suppress this header, and many "Internet Security" programs also remove this header.

In addition, requests invoked by a visitor clicking on a bookmark or a JavaScript-coded link will also not have an HTTP Referer header.

Because the HTTP Referer header is optional (it is not required in the HTTP protocol), these blank-referrer requests cannot be considered "malicious" in any way.

In almost all cases, the visitor will be unaware of whether his requests arrive at your server with or without a referrer. So be sure that your logic which uses the referrer value will properly handle requests arriving with a blank HTTP Referer header. Otherwise, you may inadvertently block *all* requests from large ISPs (e.g AOL and EarthLink in the US) and some corporations, and make your site look badly-broken to those visitors.

If your intention here is to provided some sort of "security" for your content, be advised that using a cookie to control access instead of the unreliable HTTP Referer would be a much-more robust solution.

Jim

Mahabub

1:45 pm on Oct 20, 2010 (gmt 0)

10+ Year Member



Dear jdMorgan,

Thanks for your solution and information, I found webmasterworld always helpful also as well few times i help some people in php section.

I know Referer is very unreliable but the client want that's the problem anyway from IE we are not getting Referer but in server logs can see Referer. Is there any way to Grab it from IE.

One more thing can you suggest me some articles or papers from where i can learn htaccess redirect method clearly.

Thanks
Mahabub

jdMorgan

3:50 pm on Oct 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The resources cited in our Apache Forum Charter (see link the the top if this page) have proven useful. Also see the tutorials and example threads in our Apache Forum Library (also linked at the top of this page).

The ability to "grab" a referrer does not depend on which browser is used. If you can see the Referer value in your logs but not in your script, then there is something wrong with the rewrite rule or with the script.

Note that the rule generates a 302-Found redirect. To generate a 301 and/or to make the code more self-documenting, specify the redirect type explicitly:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_REFERER} ^(.*)$
RewriteRule ^play/([^/]+)$ https://domain.com/$1?ref=%1 [R=301,QSA,L]

Be sure to delete your browser cache before testing any new server-side code -- config files, .htaccess files, or scripts. Otherwise, your browser may show you previously-cached stale pages and server responses, and no requests for 'fresh' pages and responses will be sent to your server until those cache entries expire (which could take months, depending on your server cache-controls and browser cache configuration).

Jim