Creating instant URL redirects using PHP without editing htaccess file

Posted on 2006-03-29
Last Modified: 2008-01-09
Hi. We're and we're looking to create instant URL redirects using our new tool TeachOutLoud ( which allows users to publish their own audio and video learning content. We want to create redirects for our authors to their author pages on the fly so if their name was Mark Twain there's a field in our system unto which they can type in their redirect to be and this URL would redirect to their author page which is something like: (a rewritten PHP URL).

Currently we're creating these redirects manually through cPanel on the back end and it's a pain. And our programmers could do it through auto-editing the htaccess file but that is dangerous as if we corrupt that file the whole site goes down. So we'd like to come up with another solution for somehow creating these redirects. They can create ones that are built onto another level like a redirect off a php pages, but we want to make it so it's right off our URL

This is for marketing purposes for our teachers and authors and for ease of access to their pages. Like MySpace does it: We can't figure out a safe way to do it, so we're looking for some one that has a solution.
Question by:jpb123
    LVL 8

    Expert Comment

    You don't have a solution then...
    You could use the "location" header to immediately process the "author page" on the server side like so:

    header("Location: /Resources/Authors-and-Narrators/Mark-Twain/225");

    Just to let you know, editing .htaccess is not dangerous for an experienced developer. And anyone should immediately test the site after any modification to .htaccess and if the site did "break" they could roll back to the previous .htaccess file. I still recommend .htaccess
    LVL 2

    Accepted Solution

    This is what I have done in the past, please take a look if it can work for your situation.

    The user is typing

    There is no such real url, the user is send to the 404 error page.
    You change this page with a php one, lets say not_found.php

    in the not_found.php set something like this.
    $author_name=substr('/', '', $_SERVER["REDIRECT_URL"];

    $sql="SELECT url FROM authors WHERE author_name = '{$author_name}'";

    if ($the_author_is_found, have the url){
     header("HTTP/1.0 200 OK");
     header("Location: ".$url);
    else {
          header("HTTP/1.0 404 Not Found");
          //do something here, display error page message

    so this worked for me, but you need to have some way to retrieve the url of the authors home page just knowing let's say "marktwain"
    you might have url like this:
    I don't know how you form this url
    but if it follows some logic you should be able redirect to this url using the same logic.

    hope this helps :)

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now