Home > database >  Random redirects on single URL
Random redirects on single URL

Time:03-26

Hi guys is this possible via .htaccess?

This URL: example.com/main should randomly redirects to one of these two links: 

1) example-one.com/test1
2) example-two.com/test2

I've tried this article but no luck: .htaccess redirect to random URL

CodePudding user response:

To base the "randomness" on the seconds portion of the current time and redirect to URL#1 on even seconds and URL#2 on odd seconds (when requesting /main) then you can do something like the following (before the existing WordPress directives):

RewriteCond %{TIME_SEC} (0|2|4|6|8)$
RewriteRule ^main$ https://example-one.com/test1 [R,L]
RewriteRule ^main$ https://example-two.com/test2 [R,L]

Importantly, this is a 302 (temporary) redirect. (A 301 would ordinarily be cached by the browser, so the same user/browser would see the same response on repeated requests - which could be desirable in some scenarios.)

NB: This isn't strictly "random" as it's tied directly to time, but might appear random to the casual user.

To implement "real" random you would need access to the server config to configure a rnd RewriteMap, which can then be called from .htaccess. But this cannot be configured in .htaccess alone.

  • Related