3

Is there a way to check using RewriteCond whether the Referer includes the requested URI or...

RewriteCond %{HTTP_REFERER} == 'host.com/' + %{REQUEST_URI} ...

?

I need to catch whether the surfer clicked a link on the page which links to the same page and change the RewriteRule based on it.

mature
  • 161
  • 2
  • 11
  • To clarify... you want to check that the user "clicked" a link on a page that links to the _same page_?! And this is sufficiently generic that it could happen on _any page_? – MrWhite Jan 15 '19 at 21:24
  • 1
    @MrWhite, right "generic that it could happen on any page". – mature Jan 15 '19 at 21:36
  • 1
    Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D Apr 16 '19 at 09:34

1 Answers1

2

Yes, you can construct a condition to do this (although it sounds a little bit weird, but hey) ...

To check that the user "clicked" a link on a page that links to the same page... in other words, whether the requested host + URL-path is a subset of the HTTP Referer request header, then you can do something like the following using a mod_rewrite condition:

RewriteCond %{HTTP_REFERER}@@%{HTTP_HOST}%{REQUEST_URI} ^https?://([a-z.-]+/[^?]*)(?:.*)@@\1$

The @@ is just a character sequence that doesn't occur in the URL and acts as a delimiter between the two parts we are trying to match.

The CondPattern (regex) uses a backreference (\1) to check that the captured subgroup in the Referer pattern matches HTTP_HOST + REQUEST_URI.

If your site is only HTTPS then you can remove the ? in ^https?://.

Note that this explicitly ignores any query string that might be on the HTTP_REFERER, only the URL-path and hostname is checked.

Just need to add the obligatory note regarding the HTTP Referer request header... it is not 100% reliable. The user-agent may not send the Referer header when one is expected and it is easily faked if a user is inclined to do so.

MrWhite
  • 11,643
  • 4
  • 25
  • 40