View Full Version : 301 redirect without using htaccess
a12c4magic
18-08-2005, 10:57/10:57AM
This maybe of use to others.
For those with an oscommerce site whose host won't allow mod_rewrite for whatever reason.
In my includes\application_top.php right after <?php
I placed this code:
function permanent_redirect($to) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://$to");
exit();
}
if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') {
permanent_redirect('www.mydomain.co.uk.');
}
and it works perfectly (obviously I put the correct domain name in but for the forum I have changed it to mydomain)
Now you still might want to look at using the tep_href_link function with this as now if someone requests mydomain.co.uk/index.php?products_id=12
they will be redirected to www.mydomain.co.uk (without the parameters).
So use the tep_href_link..... so that mydomain.co.uk/index.php?products_id=12 becomes www.mydomain.co.uk/index.php?products_id=12
The above part I’m still trying to figure out and will post the results once I have it working correctly.
a12c4magic
18-08-2005, 11:31/11:31AM
Ok this seems to work just fine
In my includes\application_top.php right after <?php
function permanent_redirect($to) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://$to");
exit();
}
if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') {
permanent_redirect('www.mydomain.co.uk' . $_SERVER['REQUEST_URI']);
}
Please note I have changed part of the code so now if someone requests mydomain.co.uk/index.php?products_id=12
they will be redirected to www.mydomain.co.uk/index.php?products_id=12 ( or with whatever the parameters are).
I can not take the credit for this as I had help from boxtel, where would we be without help from others :)
a12c4magic
21-08-2005, 07:38/07:38AM
Having solved the http://mysite.co.uk redirect to http://www.mysite.co.uk
I now need to solve a further redirect, I have 3 urls which are the same page
http://www.mysite.co.uk/index.php?cPath=4
http://www.mysite.co.uk/index.php?cName=mtg-singles-4th-edition
http://www.mysite.co.uk/index.php?sort=4a&page=1&cName=mtg-singles-4th-edition&category=4th_Edition
I need to redirect to:
http://www.mysite.co.uk/index.php?cName=mtg-singles-4th-edition
I need to figure out how to achieve this because search engines see these 3 different urls as duplicate content and I have lots of pages with the same problem.
Here's the exisitng code I'm using in my application_top.php
<?php
function permanent_redirect($to) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://$to");
exit();
}
if ($_SERVER['HTTP_HOST'] == 'mysite.co.uk') {
permanent_redirect('www.mysite.co.uk' . $_SERVER['REQUEST_URI']);
}
I’ll keep trying to solve this problem, however if anyone comes up with a solution before I post one please let me know.
a12c4magic
22-08-2005, 22:01/10:01PM
Rather than a redirect I figure I will have to grab the GET variables, remove the ones I don't want and reconstruct the urls.
I'll have this sorted before long :)
ihelpyou
22-08-2005, 22:55/10:55PM
Keep it up. Members are reading even if no one replies in this thread. :)
WebSavvy
22-08-2005, 23:34/11:34PM
Sue, I haven't tested the code below, but in all probability it should function. I've not done that many redirects with PHP (I have mod_rewrite and CPanel 10).
<?php
$location = ($REQUEST_URI);
$dom = "mydomain.com";
$to = "mydomain.co.uk";
if(preg_match("/$dom/i", "$location")){
function permanent_redirect($to) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://$to");
exit();
}
}
?>
WebSavvy
23-08-2005, 01:04/01:04AM
Sue, with regard to the other two URIs, you can block them using wildcards in robots.txt (for Google User-agent)
See here for information:
http://www.searchengineworld.com/misc/robots_txt_crawl.htm
You'd block them in the following manner via robots.txt
User-agent: Googlebot
Disallow: /*index.php?cPath
Disallow: /*index.php?sort
At the time of this writing, only Googlebot is known to support robots.txt wildcards, though other bots may adopt this at some future point.
a12c4magic
23-08-2005, 05:45/05:45AM
Debs a phone call last night to g1smd resulted in adding this to my robots.txt
Disallow: /index.php?cPath
Disallow: /index.php?sort
so great minds do think alike :)
I'll take a look at the rewrite code, however I still feel that stipping the unwanted code out would be a better solution, I'll let you know the results once I'm happy with them.
WebSavvy
23-08-2005, 05:53/05:53AM
g1 makes me laugh. :)
He just found out a while ago that google supports wildcards. ;) He's a nice guy though, and likes to help. :)
Try the chunk of code in my post further up and see if it helps?
I'll have a bit more time in a few days (right now working on other code) but after I finish, I might be able to hack something together for you. Shouldn't take long.
Does your shopping cart create dynamic URIs on the fly or are they hard coded and stored in the db in a URI field?
If I know how it's done, I'm better able to help.
ArmenT
23-08-2005, 11:25/11:25AM
Originally posted by savvy1
Sue, I haven't tested the code below, but in all probability it should function. I've not done that many redirects with PHP (I have mod_rewrite and CPanel 10).
<?php
$location = ($REQUEST_URI);
$dom = "mydomain.com";
$to = "mydomain.co.uk";
if(preg_match("/$dom/i", "$location")){
function permanent_redirect($to) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://$to");
exit();
}
}
?>
Note: Your code won't work if register_globals is set to off (This setting is defaulted to off since PHP 4.2 for security purposes, but several people turn it back on for no good reason.) You should really use $_SERVER['REQUEST_URI'] instead :).
Also, why preg_match()? A simple strpos() probably a lot faster and lot lighter on the server too.
a12c4magic: You could check $_SERVER['QUERY_STRING'] and redirect accordingly.
<?php
$qry = $_SERVER['QUERY_STRING'];
if ($qry == "sort=4a&page=1&cName=mtg-singles-4th-edition&category=4th_Edition"
|| $qry == "cPath=4")
PermanentRedirect("to/somewhere/else");
?>
a12c4magic
23-08-2005, 11:30/11:30AM
Originally posted by ArmenT
Note: Your code won't work if register_globals is set to off (This setting is defaulted to off since PHP 4.2 for security purposes, but several people turn it back on for no good reason.) You should really use $_SERVER['REQUEST_URI'] instead :).
Also, why preg_match()? A simple strpos() probably a lot faster and lot lighter on the server too.
a12c4magic: You could check $_SERVER['QUERY_STRING'] and redirect accordingly.
<?php
$qry = $_SERVER['QUERY_STRING'];
if ($qry == "sort=4a&page=1&cName=mtg-singles-4th-edition&category=4th_Edition"
|| $qry == "cPath=4")
PermanentRedirect("to/somewhere/else");
?>
There way too many urls to be redirected to do it that way, besides (correct me if I’m wrong) I thought search engines would only go through 50 redirects then ignore the rest
WebSavvy
23-08-2005, 11:38/11:38AM
Note: Your code won't work if register_globals is set to off (This setting is defaulted to off since PHP 4.2 for security purposes, but several people turn it back on for no good reason.)
They're on, on my server. ;)
You should really use $_SERVER['REQUEST_URI'] instead :).
Also, why preg_match()? A simple strpos() probably a lot faster and lot lighter on the server too.
At 3:00 AM (or so) when I tried helping Sue ... it made sense. LOL.
a12c4magic
23-08-2005, 13:32/01:32PM
Debs,
Don't sweat it you weren't to know if my register globals were set to on or off.
Heck I still don't know *ROFL
g1smd
23-08-2005, 17:26/05:26PM
>> I thought search engines would only go through 50 redirects then ignore the rest <<
Nearly. Google only reads the first 50 lines of the robots.txt file.
a12c4magic
25-08-2005, 00:08/12:08AM
I’d give the answer or at least I’d try to
But I’m unable because I don’t have a clue
I look at the pixels dancing across my screen
But they just stare back at me looking really mean
All the code displayed in front of me
Has me so confused can’t you see
I would have to say I was being thick
But I’ve at least one brain cell more than a brick
Maybe that makes me pond life
Or just not as sharp as the average knife :D
a12c4magic
26-08-2005, 05:22/05:22AM
im my includes/modules/product_listing.php. Line 65 should read:
$lc_text = tep_create_sort_heading($HTTP_GET_VARS['sort'], $col+1,
$lc_text);
I change it to:
$lc_text = tep_create_sort_heading(0, $col+1, $lc_text);
The above does not prevent a URL with a sort= from working.
Howerver if you go to one of my category pages you will see that the column headers such as Product Name and Price are no longer links and don't have the little up or down arrow next to them.
This means that search engines won't find the sort links on my site anymore.
The redirect code that will take care of incoming URLs with sort will be added soon *fingers crossed
g1smd
26-08-2005, 11:49/11:49AM
The new attribute rel="nofollow" can also be used on links that you don't want to be indexed.
a12c4magic
31-08-2005, 11:53/11:53AM
The following code will put in $newurl the URL you want to redirect to. It removes sort and cPath, and if both category and cName are provided, removes category. It does not rewrite cPath to a cName and does nothing for your product names.
Place the code in includes/application_top.php
just below this code:
// define how the session functions will be used
require(DIR_WS_FUNCTIONS . 'sessions.php');
$oldgets = tep_get_all_get_params();
$exclude = array('sort','cPath');
if (isset($HTTP_GET_VARS['category']) && isset($HTTP_GET_VARS['cName'])) $exclude[] = 'category';
$newgets = tep_get_all_get_params ($exclude);
$newurl = (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . $_SERVER['PHP_SELF'];
if ($newgets != '') $newurl .= '?' . $newgets;
if ($newgets != $oldgets) permanent_redirect($newurl);
I assume Google will drop all the pages that it can not longer reach and as a result I will not be putting out duplicate content.
g1smd
31-08-2005, 16:45/04:45PM
Sounds like that is close to getting the job done.
a12c4magic
31-08-2005, 18:26/06:26PM
G1 not yet I still have some kinks to sort out such as this url:
www.mysite.co.uk/index.php?cName=mtg-token-cards&category=MTG_Token_Cards
redirects to this url:
http://www.mysite.co.uk/index.php?cName=mtg-token-cards&
Notice the & at the end it shouldn't be there, so still some editing to go with get_all_get_params routine to eliminate that &
Also this url: http://www.mysite.co.uk/index.php?cPath=19
redirects to:
http://www.mysite.co.uk/index.php
Not what I want at all so for now I've changed
array('sort','cPath')
to:
array('sort')
So the Cpath= links are still there until I solve find the right solution.
Rome wasn't built in a day although it did burn down in one *L
a12c4magic
02-09-2005, 05:07/05:07AM
See Ultimate SEO URLs for oscommerce Version 2.0 thread for the outcome.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.