How do you make this 301 redirect using php only?

Rebecca May 24, 2010
Pinterest Stumbleupon Whatsapp

I would like www enforced and file structure intact.

e.g. http://subdomain.domain.com/ants.php redirects to http://www.newdomain.org/ants.php
e.g. http://newdomain.org/dogs.php redirects to http://www.newdomain.org/dogs.php

Would I also use an add-on domain? Or would a parked domain be better?

Ads by Google

  1. Tina
    July 2, 2010 at 6:10 pm

    Rebecca,

    were Aibek's or Rob's pointers helpful? Please let us know whether your found a solution to your question. Thank you!

  2. rob
    May 29, 2010 at 7:19 pm

    OK, I know you said PHP, I'll come on to that but first consider whether you can use the .htaccess file
    This does call for a good understanding of .htaccess
    The official documentation isn't for the faint hearted so look for internet tutorials in something approaching normal English! Example: http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

    for example in htaccess forcing www would look like this (untested)
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
    RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

    OK so now for PHP, not sure why Stev C suggestion of

    isn't what you want so my answer may get you no further forware either.

    I'll assume you do know PHP so only outline how I'd try to tackle it:
    Create a redirect.php, it will only run as a server side program and never displays anything to the browser, instead it captures and redirects other requests.
    Read one of the system variables like _SERVER["SCRIPT_URI"] that will give you a string something like http://subdomain.domain.com/ants.php
    Have a switch statement to switch according to the value of that variable.

    The individual cases should then say something like:
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header("Location: http://www.newdomain.org/ants.php");
    break;

    Now replace each web page you want redirected with a copy of redirect.php so when someone requests http://subdomain.domain.com/ants.php the server runs a copy of redirect.php which has been renamed as ants.php and serves up the new page at http://www.newdomain.org/ants.php

    You could make more complicated decisions by slicing the _SERVER response string. For example you could do a redirect of any without the www element to the same page with www - but not on the same domain as the individual page name is the same whether the domain has www prefix or not.

    Not sure MUO is the best place for a programming question like this though, you may do better on a PHP specialist forum.

  3. Aibek
    May 29, 2010 at 1:38 pm

    Do you want to redirect just one page or an entire website? D you have an access to the the server of the http://subdomain.domain.com ?

    • Rebecca
      May 29, 2010 at 9:32 pm

      I want to redirect an entire website!

      Hmm, server? Well I can modify files from cpanel and through http://ftp...

      • Aibek
        May 30, 2010 at 11:14 am

        Rebecca,

        It should be fairly easy to do using .htaccess file placed in the directory corresponding to subdomain.domain.com on your server. (It can be accessed via Cpanel or FTP)

        If you're running WordPress there are very a bunch of simple WordPress plugin that can do that for you.

  4. Steve Campbell
    May 24, 2010 at 7:07 pm

    Rebecca,

    This should help you out some:

    http://www.webconfs.com/how-to-redirect-a-webpage.php

    I'm not sure it makes a difference how you have your domain. Best to just try it out and see what works.

    • Rebecca
      May 28, 2010 at 6:28 pm

      I tried that. That code does take me to the new URL but it doesn't carry the files over.

Ads by Google