Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

Current site:

New site:

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

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location:" );

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.


share|improve this question
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):


# 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:$folder\");"
    } > dept/$folder/index.php

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 and do a 301 to – 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

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
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


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.