This method is helpful if you need to redirect on a small scale and using .htaccess or mod_rewrite are not an option. My situation whe I needed to use this was when I was being hosted on an IIS server that did not allow URL rewriting. If anyone finds themselves in this position, hopefully this will be useful to you. It is really quite simple but would probably not be useful for larger sites.
Create a file in your root directory called redirect.php that looks like the code posted below.
<?php
$redirect = false;
$path = $_SERVER['REQUEST_URI'];
if ($path == '/oldpath.php') {
$path = '/newpath';
$redirect = true;
}
$url = $_SERVER['HTTP_HOST'];
if ($url != 'www.yoursite.com') {
$redirect = true;
}
if ($redirect==true){
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.yoursite.com' .$path);
}
?>
For each URL you need to redirect from simply add another:
if ($path == '/oldpath.php') {
$path = '/newpath';
$redirect = true;
}
You could alternatively also use this to redirect from a www url to a non www version of your site by replacing the last two blocks with:
$url = $_SERVER['HTTP_HOST'];
if ($url != 'yoursite.com') {
$redirect = true;
}
if ($redirect==true){
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://yoursite.com' .$path);
}
And at the top of the page.tpl.php that you're using add the line:
<?php require('redirect.php'); ?>
Again, the code posted "as is" is probably not the best solution if you are converting a large site as each url is listed individually, although if you are handy with regular expressions(which I am not), you might be able to tweak the code a bit to make it work for you.
Cheers -
David
http://www.flipoutdesign.com
Comments
Mcuked Up
Looks like the code got a bit mucked up. I will try to re-post the first part
David
http://www.flipoutdesign.com
Location is related to HTTP,
Location is related to HTTP, why do you put a HTML link there ?!?
Location should be followed by an URL, and not a HTML link ... It's not the same thing.
Lets try once more
I havent posted code to this forum before so I am sill learning the nuances of what it does to my code. Sorry, you are correct. There should not be the a tag included in the location. Let me see if I can get this to look correct.
Just ignore the spaces in the header location URL. It's the only way I could keep it from adding the formatting to it.
Flipout Design
http://www.flipoutdesign.com
Using something similar for SSL
Hi guys,
I'm trying to do something similar, such that I want to redirect certain pages to SSL, and redirect the pages that are not supposed to be secure, back to http (FYI, I tried Secure Pages, and it's way too slow on my site).
Specifically, the pages that I would like to be secure are:
* www.example.com/user*
* www.example.com/admin*
* www.example.com/node/[node-id#]/edit
* www.example.com/node/[node-id#]/delete
* www.example.com/webform1
* www.example.com/webform2
* www.example.com/secure1/*
* www.example.com/secure2/*
Here's what I've done so far:
In sites/all/themes/active/page.tpl.php, I've added at line 1
<?php require('redirect.php');?>Here's sites/all/themes/active/redirect.php
Unfortunately, this does not seem to work, as the "secure pages" do not redirect. I have tried to see if it was a cache problem, and cleared the cache with the devel module ... still nothing. Would someone mind helping me figure out what is wrong with my code? FYI, I use the clean urls module.
Many thanks in advance.