0

I have a setup where I use the following to rewrite any page that uses the .html ext to use a .php file that is located inside the products folder.

This works fine, but since it is rewriting - I am not able to grab the referer page that the user clicks on to get to the page - links/products/whatever.html.

If i do not use this rewrite rule, I can see the referer as expected, but not with the following code.

Is there a way for me to use the following yet still allow the referer to be accessed?

#used to rewrite any url with links/products/whatever.html to use links/products/index.php
RewriteEngine On
RewriteRule ^(links)/(products)(/.*)?$ links/products/index.php [L]
Scott
  • 17
  • 4
  • 1
    You shouldn't rely on the referrer header in the first place, there's plenty about that on the web. – php_nub_qq Mar 10 '22 at 15:41
  • 4
    There must be something else going on. An internal rewrite does nothing to change the `Referer` header (ie. `$_SERVER['HTTP_REFERER']`). – MrWhite Mar 10 '22 at 15:43
  • Im not relying on the referer, but I am using it if it is there. – Scott Mar 10 '22 at 15:45
  • Well in that case - what @MrWhite said - I suspect you once followed a link another time entered the address manually and assumed it was due to the rewrite. – php_nub_qq Mar 10 '22 at 15:46
  • _Aside:_ "I use the following to rewrite any page that uses the .html ext" - that code does not specifically check for the `.html` ext. That rule simply rewrites any URL that starts `/links/products`. In fact, it even rewrites to itself! – MrWhite Mar 10 '22 at 15:46
  • 1
    "Can't get $_SERVER['HTTP_REFERER'] with RewriteCond" - But you aren't using a `RewriteCond` directive in the code you've posted? And you can't use `$_SERVER['HTTP_REFERER']` (a PHP variable) with `RewriteCond` anyway? – MrWhite Mar 10 '22 at 15:52
  • For referrers in htaccess see https://stackoverflow.com/questions/13106299/redirect-using-htaccess-based-on-referrer. – user3783243 Mar 10 '22 at 15:56
  • If I remove the rewrite rule, and then just link to a file names /links/products/produuct1.php - it shows the referer. – Scott Mar 10 '22 at 15:57
  • So if I just link to a page like /links/products/produuct1.php - I can get the referer no problem But If I link to /links/products/produuct1.html and have the rewrite rule use the index.php file instead of the product1.html, it does not show the referer. So is there another way for me to accomplish what I need and still have access to the referer? – Scott Mar 10 '22 at 16:12
  • Do you have any other directives in your `.htaccess` file? Any other `.htaccess` files in subdirectories? If you need to rewrite the URL then something like what you have done is the only way to do this. And this should not lose the `Referer`. Unless your server is doing something funny with the request?! But, as mentioned, your rule is not strictly correct if you are wanting to rewrite `.html` requests and the rule you've posted also rewrites itself. Try `RewriteRule ^links/products/[^/.]\.html$ links/products/index.php [L]`. – MrWhite Mar 10 '22 at 16:28
  • 1
    Appreciate the help guys. I did find something else in a hidden htaccess file that was causing the issue. Got it working fine now. – Scott Mar 11 '22 at 15:00

0 Answers0