What is the best way to forward a single URL to several different URLs simultaneously?

I've searched everywhere but can't seem to find the solution.

What I'm looking for is :
Whenever a user visits mysite.com, I'd like to redirect them to a different URL each time, chosen randomly.

For instance:
When user A visits mysite.com, they should be redirected to google.com.
When user B visits mysite.com, send them to facebook.com.
And when user C visits mysite.com, direct them to youtube.com.
The cycle then resets and continues with other users.

1st user -> redirects to google.com
2nd user -> redirects to facebook.com
3rd user -> redirects to youtube.com
reset cycle
4th user -> redirects to google.com
5th user -> redirects to facebook.com
and so on....

Edit : My website is a simple one built using html and css.

Answer №1

It seems like you're looking to achieve this using a basic HTML + CSS frontend approach. Typically, this task would be handled on the backend server by either storing visitor counts or randomizing redirect URLs.

However, if you don't have control over the backend, there is another option available. You can incorporate a simple JavaScript file/script that executes the redirect. This method involves creating a landing page (index.html) that automatically redirects to your desired destination once loaded.

If you'd like to mimic a redirect process, you can use the following code snippet:

// Mimicking HTTP redirect behavior
window.location.replace("http://stackoverflow.com");

To implement this in your HTML file, you can include something similar to the following script (inside the < body > tag):

<!DOCTYPE html>
<html>
<body>
<script>
var sites = ['http://www.google.com', 'http://www.site2.com'];
window.location.replace(sites[Math.floor(Math.random()*sites.length)]);
</script>
</body>
</html>

This approach should work effectively for your needs, keeping in mind that the redirection will occur randomly rather than sequentially. By utilizing this method, you can bypass the need for backend integration :).

Feel free to expand the array with additional website URLs, separated by commas, to further customize the redirection process.

Answer №2

If you're working in PHP, here's a handy snippet of code that achieves the desired outcome:

$websites = ["www.google.com","facebook.com", "youtube.com"];
if (apc_exists('app:index') !== false)
{
    $indexValue = apc_get('app:index');
    $indexValue++;
}
else
{
    $indexValue = 0;
    apc_store('app:index', $indexValue);
}
header('Location: http://'.$websites[$indexValue%3]);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using .htaccess to redirect all traffic through a PHP file

RewriteEngine on RewriteCond %{REQUEST_URI} !(\.|/$) RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ api.php?url=$1 [L] This is the current configuration I am using ...

Exploring the Combination of Conditional Rendering and Redux within the App.js File

Currently, I am in the process of setting up an authentication flow with Redux in my application. To control the display of either the app screen or the authentication screen, I have implemented conditional rendering in my App.js file. Here is the snippet ...

JavaScript - Modifying several object properties within an array of objects

I am attempting to update the values of multiple objects within an array of objects. // Using a for..of loop with variable i to access the second array and retrieve values const AntraegeListe = new Array(); for (let i = 0; i < MESRForm.MitarbeiterL ...

Ways to select a random row from a MySQL database

<button onclick="myUniqueFunction()">Press here!</button> <?php function myUniqueFunction() { $con = mysqli_connect("mysql.hostinger.no", "u452849516_altge", "password", "u452849516_altge"); $query = "S ...

Individual files dedicated to each controller in Angular.js are a common practice in development

In my project, I am looking to create an Angular module with a main controller, as well as additional controllers that are stored in separate files. Main module: var app = angular.module("main", []); app.controller("mainCtrl", function ($scope) { $scop ...

Is the `ng-model` for the AngularStrap `bs-typeahead` component not accessible in the current scope?

I am encountering a problem with the ng-model value in an element that uses AngularStrap's bs-typeahead. It seems to not be accessible within the scope, but it works fine when accessed using {{ var }} in the HTML. Here is the HTML code: <input ty ...

What is the method for obtaining the values from these newly generated text fields?

Every time I click on the "ADD TEXTBOX" button, a new HTML textbox is dynamically created using jQuery's append method. However, I am struggling to figure out how to retrieve and store the values of these textboxes in PHP. $(w).append('<div&g ...

activate a CSS-only modal with JavaScript

Is it possible to trigger a pure CSS modal using JavaScript (jQuery) without the need for a label, so that it activates when a user visits a page? http://jsfiddle.net/h84nubzt/ <label class="btn" for="modal-one">Example</a> <!-- Modal ...

Interactively retrieve data from a nested field in mongoDB

Querying my mongoDb is my current task, but I'm facing a challenge with creating a dynamic query. Each user has a different set of preferences, making it tricky to create a standardized query. dynamicArray = [ 'sample1', 'sample ...

Adding a static global constant in webpack dynamically

I'm facing a challenge with adding a global constant to my project using webpack.DefinePlugin. I've successfully added one in the module.exports, but I struggle to do this conditionally. When I declare and use '__VERSION__' in my module ...

What is the process for establishing an if condition when there are no results returned?

Greetings, I am seeking assistance with the following issue: // Add email later on let sqlSelectBoxInformation = "SELECT DISTINCT longestDimension, box_id from box WHERE occupied ='unoccupied'"; connectionBoxInformation.query(sqlSelectBoxInfo ...

I am experiencing difficulties with my focusin not functioning properly for the input element that I added

I am working on a program that includes a dynamic table with the following structure: <table id="selectedItems"> </table> I plan to use jQuery to append elements to it like so: var i=0; $("#selectedItems").html(''); $("#sel ...

Removing an item from an array while also updating one of its attributes in JavaScript

I am facing a challenge with this list of objects: const flights = [ { id: 00, to: "New York", from: "Barcelona", cost: 700, scale: false }, { id: 01, to: "Los Angeles", from: "Madrid", cost: 1100, scale: tru ...

javascript issue with Q promise not passing complete parameters

I am struggling to pass all three arguments successfully in my promise callback. The issue I'm facing is that only one argument is being received instead of the expected three: var asyncFunction= function(resolve) { setTimeout(function() { ...

Is there a way to trigger a function from a specific div element and showcase the JSON data it retrieves in

I am working with a React JS code page that looks like this: import React, { useState } from "react"; import { Auth, API } from "aws-amplify"; function dailyFiles(props) { const [apiError502, setApiError502] = useState(false); // Extracted into a re ...

Checking for Three.js WebGL compatibility and then switching to traditional canvas if necessary

Is there a way to check for WebGL support in three.js and switch to a standard Canvas render if not available? I'd appreciate any insights from those who have experience with this library. ...

Tips for displaying the message "{"POWER":"ON"}" within the else if statement (this.responseText == ({"POWER":"ON"})) {

Hey everyone, I'm trying to adjust the color of a button on my webpage based on the response I receive from this.responseText. I understand the JSON response, but for some reason, I can't seem to incorporate it into my code. If anyone could lend ...

NodeJS application experiencing significant delays due to slow response times from MySQL database queries

Currently, I am in the process of learning about Node.js. Through utilizing Express and Node-Mysql, I have been able to effectively query my mysql database and return the outcomes as JSON to the client. However, there seems to be a significant delay. Eve ...

AngularJS Partial Views: Enhancing Your Website's User Experience

As a newcomer to the world of Angular, I am seeking guidance on a specific issue. I have encountered a one-to-many relationship scenario where one Category can have multiple Product items. The challenge lies in my layout page setup where I display various ...

Utilizing Socket.io alongside Express.js routes for real-time communication

I have been working with socketio and expressjs, using routes as middleware. However, my current setup is not functioning as intended. I have provided the code snippets below, but none of them seem to be working. Can someone please help me troubleshoot thi ...