Skip to main content
Update with respect to comments - implemented an internal rewrite. Removed QSD flag
Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90
RewriteEngine On

RewriteCond %{HTTP_REFERER} \?.*\bbikes=1\b [NC]
RewriteRule ^product-configurator$ /bikes-configurator [QSD,R=307[R=307,L]

If this is intended to work for "any traffic" (as suggested in a comment), not just requests for /product-configurator then change the RewriteRule pattern to something like ^ instead. The QSD flag is then required in order to remove the query string from the redirected request, to avoid a redirect loop.

However, it's not clear why you would need to "redirect" the request since this is all happening on the same server. You could potentially internally rewrite the request instead (no external redirect, no second request, quicker and more efficient all round). However, /product-configurator and /bikes-configurator require further routing to get to your PHP script - so "rewriting" to /bikes-configurator is not correct.

UPDATE: In that case, it would be /product-configurator.php

Assuming you are just using extensionless URLs and the underlying file that handles the request is simply /bikes-configurator.php then you can perhaps implement an internal rewrite instead.

For example, instead of the above, this would be written as:

RewriteCond %{HTTP_REFERER} \?.*\bbikes=1\b [NC]
RewriteRule ^product-configurator$ /bikes-configurator.php [L]

The URL in the address bar stays as /product-configurator, but the request is internally rewritten (note the absence of the R flag) to /bikes-configurator.php when bikes=1 is included in the query string of the HTTP Referer. There is no external redirect.

This may also be solvable directly in your PHP script - no additional redirect/rewrite in .htaccess required. (Although from your recent comments, it looks like the request /bike-configurator is simply extensionless and the request maps to the PHP file /bike-configurator.php? The logic that tests the HTTP_REFERER would need to be in /product-configurator.php - you then make the necessary PHP calls to pass control to /bike-configurator.php if the condition is met - but exactly how you do this depends on the structure of your app.)

RewriteEngine On

RewriteCond %{HTTP_REFERER} \?.*\bbikes=1\b [NC]
RewriteRule ^product-configurator$ /bikes-configurator [QSD,R=307,L]

If this is intended to work for "any traffic" (as suggested in a comment), not just requests for /product-configurator then change the RewriteRule pattern to something like ^ instead. The QSD flag is then required in order to remove the query string from the redirected request, to avoid a redirect loop.

However, it's not clear why you would need to "redirect" the request since this is all happening on the same server. You could potentially internally rewrite the request instead (no external redirect, no second request, quicker and more efficient all round). However, /product-configurator and /bikes-configurator require further routing to get to your PHP script - so "rewriting" to /bikes-configurator is not correct.

This may also be solvable directly in your PHP script. (Although from your recent comments, it looks like the request /bike-configurator is simply extensionless and the request maps to the PHP file /bike-configurator.php?)

RewriteEngine On

RewriteCond %{HTTP_REFERER} \?.*\bbikes=1\b [NC]
RewriteRule ^product-configurator$ /bikes-configurator [R=307,L]

If this is intended to work for "any traffic" (as suggested in a comment), not just requests for /product-configurator then change the RewriteRule pattern to something like ^ instead.

However, it's not clear why you would need to "redirect" the request since this is all happening on the same server. You could potentially internally rewrite the request instead (no external redirect, no second request, quicker and more efficient all round). However, /product-configurator and /bikes-configurator require further routing to get to your PHP script - so "rewriting" to /bikes-configurator is not correct.

UPDATE: In that case, it would be /product-configurator.php

Assuming you are just using extensionless URLs and the underlying file that handles the request is simply /bikes-configurator.php then you can perhaps implement an internal rewrite instead.

For example, instead of the above, this would be written as:

RewriteCond %{HTTP_REFERER} \?.*\bbikes=1\b [NC]
RewriteRule ^product-configurator$ /bikes-configurator.php [L]

The URL in the address bar stays as /product-configurator, but the request is internally rewritten (note the absence of the R flag) to /bikes-configurator.php when bikes=1 is included in the query string of the HTTP Referer. There is no external redirect.

This may also be solvable directly in your PHP script - no additional redirect/rewrite in .htaccess required. (Although from your recent comments, it looks like the request /bike-configurator is simply extensionless and the request maps to the PHP file /bike-configurator.php? The logic that tests the HTTP_REFERER would need to be in /product-configurator.php - you then make the necessary PHP calls to pass control to /bike-configurator.php if the condition is met - but exactly how you do this depends on the structure of your app.)

Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90

To externally redirect the request from /product-configurator (I assume the missing u was a typo?) to /bikes-configurator when the HTTP Referer contains bikes=1 in the query string part of the URL then you could do something like the following using mod_rewrite near the top of your .htaccess in the document root:

RewriteEngine On

RewriteCond %{HTTP_REFERER} \?.*\bbikes=1\b [NC]
RewriteRule ^product-configurator$ /bikes-configurator [QSD,R=307,L]

The RewriteCond directive checks for the URL parameter bikes=1 anywhere in the query string of the Referer HTTP request header.

If this is intended to work for "any traffic" (as suggested in a comment), not just requests for /product-configurator then change the RewriteRule pattern to something like ^ instead. The QSD flag is then required in order to remove the query string from the redirected request, to avoid a redirect loop.

The 307 status is required for the user-agent to preserve the HTTP method in the redirected request.

Note that this redirects all requests, POST and GET. You could specifically redirect only POST requests if you wish by checking against the REQUEST_METHOD server variable.

The 3rd party does nothing except POST to my site

The "3rd party" needs to be able to follow redirect responses (and submit the 2nd request). An ordinary HTML form submission in a browser will naturally follow the redirect. However, a scripted CURL (for example) may not.


However, it's not clear why you would need to "redirect" the request since this is all happening on the same server. You could potentially internally rewrite the request instead (no external redirect, no second request, quicker and more efficient all round). However, /product-configurator and /bikes-configurator require further routing to get to your PHP script - so "rewriting" to /bikes-configurator is not correct.

This may also be solvable directly in your PHP script. (Although from your recent comments, it looks like the request /bike-configurator is simply extensionless and the request maps to the PHP file /bike-configurator.php?)