Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8394707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:07:38+00:00 2026-06-09T20:07:38+00:00

I have a quick question that should be simple for most people here. On

  • 0

I have a quick question that should be simple for most people here. On a website, I am redirecting all my links through 1 simple PHP redirect called visit.php

I am formatting my outbound redirects like this..

visit.php?url=google.com
visit.php?url=yahoo.com
visit.php?url=aol.com

Then my visit.php looks like this…

<?php
$url = htmlspecialchars($_GET['url']);

if($url == 'google.com'){
header("Location: http://google.com");
}

if($url == 'yahoo.com'){
header("Location: http://yahoo.com");
}

if($url == 'aol.com'){
header("Location: http://aol.com");
}

else {
header("Location: http://mydomain.com/404");
}

The problem I am having is with the else statement. When I delete it from the script, everything works as expected. When I put it in (so any misspellings redirect to a 404) it overrides all the links I defined in the script and redirects to the 404.

Another problem I am having is when the ?url= variable is not used. If a user on my site copies the link to their clipboard and changes it to anything but ?url= I’d like to redirect to the 404. The code I have tried using to accomplish this looks like this…

if(!isset($_GET['url'])){
header("Location: http://mydomain.com/404");
}

My logic is… if the ?url= variable is not set, just redirect to 404 because someone must be playing around. If somebody loads visit.php?id=blah it should just redirect to the 404.

However, I am not getting the expected behavior. I am pretty new to PHP and programming in general… so forgive me if these questions are kindergarten level. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:07:39+00:00Added an answer on June 9, 2026 at 8:07 pm

    You should be using the if/else if/else construct otherwise that else statement is attached only to the last if.

    When the condition of that last if is not fulfilled it’ll naturally execute the else clause.

    Your initial approach (corrected):

    <?php
    $url = htmlspecialchars($_GET['url']);
    
    if($url == 'google.com'){
        header("Location: http://google.com");
    }
    
    else if($url == 'yahoo.com'){
        header("Location: http://yahoo.com");
    }
    
    else if($url == 'aol.com'){
        header("Location: http://aol.com");
    }
    
    else {
        header("Location: http://mydomain.com/404");
    }
    

    Usual approach:

        <?php
    $url = htmlspecialchars($_GET['url']);
    
    if($url == 'google.com'){
        header("Location: http://google.com");
        exit;
    }
    
    if($url == 'yahoo.com'){
        header("Location: http://yahoo.com");
        exit;
    }
    
    if($url == 'aol.com'){
        header("Location: http://aol.com");
        exit;
    }
    
    header("Location: http://mydomain.com/404");
    exit;
    

    Best (recommended) approach:

    switch($url) {
        case 'google.com':
            header("Location: http://google.com");
            break;
        case 'yahoo.com':
            header("Location: http://yahoo.com");
            break;
        case 'aol.com':
            header("Location: http://aol.com");
            break;
        default:
            header("Location: http://mydomain.com/404");
            break;
    }
    

    Hmmm… better yet:

    $list_of_places = array(
        'google.com',
        'yahoo.com',
        'aol.com'
    );
    
    if(in_array($url, $list_of_places)) {
        header("Location: http://".$url);
        exit;
    }
    else {
        header("Location: http://mydomain.com/404");
        exit;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that should be quick and easy for you guys to
I have a quick question here. I know that the cakePHP find('first') function returns
Quick question. I have an app that use a native DLL through PInvoke, this
Quick question, I have a simple javascript problem that I don't know the fix
Hey guys really quick question, I have a simple test that I was doing
I have a quick linq question. I have a stored proc that should return
I have a simple question, which should hopefully have a quick answer. The code
I have a quick question that I can't seem to find online. I am
just a question that needs a quick answer, I have a Action, lets say,
Quick question... I have a query that checks for duplicates that looks like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.