Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Using php, how might I redirect all pages under a folder to a different domain?

Current site:

http://www.example.org/dept
http://www.example.org/dept/stuff
http://www.example.org/dept/more
http://www.example.org/dept/more/stuff

New site:

http://www.example-too.org/pets/stuff
http://www.example-too.org/pets/more
http://www.example-too.org/pets/more/stuff

I've learned about how to redirect a single page:

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example-too.org/pets/more/stuff" );
?>

but how to apply this to dozens of pages without creating a php redirect for each one?

[edit] I understand using web server config (apache mod_rewrite) and/or .htaccess is the best way to handle multiple redirects like this, but I those options aren't available to me.

thanks.

share|improve this question

3 Answers 3

up vote 0 down vote accepted

Unfortunately, unless one PHP script is used to handle all those URL's already, you need to create a new PHP script for each URL.

You could write some script to automate the process however.

For instance, if it is a Linux server and you are able to run shell scripts, something like this could work (execute in the htdocs folder, or equal):

#!/bin/bash

# Add folders here
FOLDERS=stuff more more/stuff    

for folder in $FOLDERS; do
    {
        echo '<?php'
        echo 'header("HTTP/1.1 301 Moved Permanently");'
        echo "header(\"Location: http://www.example-too.org/pets/$folder\");"
    } > dept/$folder/index.php
done

If you are unable to run shell scripts, you could translate the script into PHP.

share|improve this answer
    
Whilst a perfectly valid solution, I'd hate to be the one who comes in to maintain it ;) –  Mark Henderson Mar 12 '10 at 0:56
    
Hopefully it won't need maintaining. It just needs to go up long enough for the search engines to register the change. After 6 months or so the whole ./dept section will be removed. –  matt wilkie Mar 12 '10 at 1:06

Worth mentioning that the better way to do this through "Redirect" command in the web server config or .htaccess

share|improve this answer
    
Absolutally. You can use a regex to match anything matching www.example.org and do a 301 to www.example-too.org –  Mark Henderson Mar 12 '10 at 0:57
    
granted, except that I don't have access to web server config and htaccess is disabled. I added a note to the main post that using those methods is preffered if avilable –  matt wilkie Mar 12 '10 at 1:01

Thanks Mikael, I modified your script a bit to deal with my html pages instead of folders:

#!/bin/env bash

DEST=http://www.example-too.org/pets
FILES=`find . |grep \.html$ -`

for xfile in $FILES; do
    file=${xfile:2}         # strip leading ./
    file=${file%%.html}.php # change extension from .html to .php
                            # comment out preceeding line to overwrite source .html
    {
        echo '<?php'
        echo 'header("HTTP/1.1 301 Moved Permanently");'
        echo "header(\"Location: $DEST/$file\");"
        echo '?>'
    } > $file
    echo created $file
done
share|improve this answer
    
You forgot the question mark in the closing PHP tag (which, by the way is optional unless you want to stick some plain HTML/text after the PHP code). –  Mikael S Mar 12 '10 at 1:30
    
thanks! correction added –  matt wilkie Mar 12 '10 at 16:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.