Jan Teo
Jan Teo

Reputation: 21

.htaccess redirect to random URL

I have several pages on my server I got traffic on and I need to redirect them to other websites. That's the easy part. The problem is that I need to mix things up and send people to the new sites randomly.

I found out it's possible to do so by time_sec, but my current knowledge isn't enough to make it work no matter what I try.

This is the code I am working with now:

RewriteCond %{TIME_SEC} ^(0|4|8|12|16|22|26|30|34|38|42|46|50|54|58)
RedirectMatch 301 /my-page-1.php https://newsite.com/page1/discount

RewriteCond %{TIME_SEC} ^(1|5|9|13|17|23|27|31|35|39|43|47|51|55|59)
RedirectMatch 301 /my-page-1.php https://newsite.com/page1/offer

RewriteCond %{TIME_SEC} ^(2|6|10|14|18|24|28|32|36|40|44|48|52|56|60)
RedirectMatch 301 /my-page-1.php https://newsite.com/page1/email

RewriteCond %{TIME_SEC} ^(3|7|11|15|19|25|29|33|37|41|45|49|53|57)
RedirectMatch 301 /my-page-1.php https://newsite.com/page1/promo

... // another of the pages ...

RewriteCond %{TIME_SEC} ^(0|4|8|12|16|22|26|30|34|38|42|46|50|54|58)
RedirectMatch 301 /my-page-2.php https://newsite.com/page2/discount

RewriteCond %{TIME_SEC} ^(1|5|9|13|17|23|27|31|35|39|43|47|51|55|59)
RedirectMatch 301 /my-page-2.php https://newsite.com/page2/offer

RewriteCond %{TIME_SEC} ^(2|6|10|14|18|24|28|32|36|40|44|48|52|56|60)
RedirectMatch 301 /my-page-2.php https://newsite.com/page2/email

RewriteCond %{TIME_SEC} ^(3|7|11|15|19|25|29|33|37|41|45|49|53|57)
RedirectMatch 301 /my-page-2.php https://newsite.com/page2/promo

So I have several "my-page-x.php" on my server where I got traffic and I need the traffic to go RANDOMLY to specific new sites.

This code, obliviously, doesn't work. I tried like a million changes, but nothing.

Can anybody help me out here, please?

Upvotes: 2

Views: 1937

Answers (1)

foobored
foobored

Reputation: 488

Nice idea to achieve the randomness.

I made 2 adjustments

1) added "$" to the line endings for the TIME_SEC condition so that the full seconds-string is checked against the value (not just the beginning)

2) Switched the RedirectMatch with the necessary rewrite counterpart RewriteRule

This should fix your approach:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} ^my-page-1\.php$
RewriteCond %{TIME_SEC} ^(0|4|8|12|16|22|26|30|34|38|42|46|50|54|58)$
RewriteRule ^.*$ https://yoururl1.com [L, R=301]

RewriteCond %{REQUEST_FILENAME} ^my-page-1\.php$
RewriteCond %{TIME_SEC} ^(1|5|9|13|17|23|27|31|35|39|43|47|51|55|59)$
RewriteRule ^.*$ https://yoururl2.com [L, R=301]

RewriteCond %{REQUEST_FILENAME} ^my-page-1\.php$
RewriteCond %{TIME_SEC} ^(2|6|10|14|18|24|28|32|36|40|44|48|52|56|60)$
RewriteRule ^.*$ https://yoururl3.com [L, R=301]

RewriteCond %{REQUEST_FILENAME} ^my-page-1\.php$
RewriteCond %{TIME_SEC} ^(3|7|11|15|19|25|29|33|37|41|45|49|53|57)$
RewriteRule ^.*$ https://yoururl4.com [L, R=301]

Upvotes: 1

Related Questions