0

I have a website which changes the content of the page depending on what the user puts into the url.

There is only one page, but it seems like there are more because of the .htaccess I have used.

The website goes

www.site.com/1 or www.site.com/482

the content then changes depending on what the url is, for example /1 would load content assigned to ID 1 and /482, load data assigned to that of ID 482.

My problem is when the user just goes to www.site.com the page still loads but the youtube clip on the page displays a fuzzy screen with the message 'invalid parameters' which is clearly unprofessional. I want the user to be redirected to a random page within my website (anything from /1 to /999) when they go to www.site.com (when there is no forward slash and a number).

I have this code:
var randomnumber = Math.round((Math.random()) * 999 + 1);
var DATA = window.location.pathname.replace('/', '');
if (typeof DATA == "undefined"){
    DATA = randomnumber;
}

but it doesn't seem to change anything

3
  • can you post your htaccess? Commented Apr 1, 2013 at 19:00
  • Is DATA what you're using to determine which content to use? Hard to tell from your sample code. Commented Apr 1, 2013 at 19:02
  • and yes it is. DATA is what I use to determine the content Commented Apr 1, 2013 at 19:03

2 Answers 2

1

If you want to redirect using javascript, you've got to set location property of window object like this

var randomnumber = Math.round((Math.random()) * 999 + 1);
var DATA = window.location.pathname.replace('/', '');
if ( DATA == ""){
    window.location = '/' + randomnumber;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use php to do the redirect in case Javascript is disabled:

<?php 
  if (!is_numeric($_SERVER['QUERY_STRING'])){
     header("Location: http://www.site.com/?".rand(1,999));
  } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.