Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

301 redirect in application_top


Halfpint

Recommended Posts

I already have the code below in my includes\application_top which redirects anyone who types http:mysite.co.uk to http://www.mysite.co.uk.

 

Can anyone please tell me how to do a 301 redirect so that urls like the ones below which are both the same page:

 

http://www.mysite.co.uk/index.php?cPath=4

 

http://www.mysite.co.uk/index.php?sort=4a&page=1&cName=mtg-singles-4th-edition&category=4th_Edition

 

becomes like this url:

 

http://www.mysite.co.uk/index.php?cName=mtg-singles-4th-edition

 

 

 

Existing code in application_top

 

 

<?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']);

}

Link to comment
Share on other sites

That'a a 302 redirect.

 

Does your code not work? On my site, I do this in .htaccess. For example:

RewriteCond %{HTTP_HOST} ^cheshiregarden.com$
RewriteRule ^(.*)$ http://www.cheshiregarden.com/$1 [R=301,L]

Link to comment
Share on other sites

That'a a 302 redirect.

 

Does your code not work?  On my site, I do this in .htaccess.  For example:

RewriteCond %{HTTP_HOST} ^cheshiregarden.com$
RewriteRule ^(.*)$ http://www.cheshiregarden.com/$1 [R=301,L]

 

I am unable to use .htaccess due to host problem, but yes my existing code works as I stated I have a redirect in place should anyone type

 

http://mysite.co.uk it will redirect them to http://www.mysite.co.uk

 

What I need is a 301 redirect so that urls like the ones below which are both the same page:

 

http://www.mysite.co.uk/index.php?cPath=4

 

http://www.mysite.co.uk/index.php?sort=4a&...ory=4th_Edition

 

becomes like this url:

 

http://www.mysite.co.uk/index.php?cName=mt...les-4th-edition

Link to comment
Share on other sites

Either you're confused or I am. The only difference between a 301 and a 302 is the hint it gives to the user agent about whether or not it should "remember" the change. You would want to use a 301 if you have made a permanent change in a URL and want search engines to forget the old one and start using the new one. A 302 is used when it's just a process thing and the user agent does not need to change its future behavior.

 

You can accomplish the change in URL with either kind of redirect.

Link to comment
Share on other sites

Let me rephrase the problem

 

I have 3 urls which are all the same page and because of this Google sees it as duplicate content, so I need to somehow prevent Google from seeing 2 of the urls

 

The urls in question are all http://

 

www.mysite.co.uk/index.php?cPath=4

 

www.mysite.co.uk/index.php?cName=mtg-singles-4th-edition

 

www.mysite.co.uk/index.php?sort=4a&page=1&cName=mtg-singles-4th-edition&category=4th_Edition

 

 

So I need the url that google finds to be this url:

 

http://www.mysite.co.uk/index.php?cName=mtg-singles-4th-edition

 

And I figured I would need some type of redirect to acheive this

Link to comment
Share on other sites

Ok. Yes, you want a 301 redirect for the ones you don't want. I don't know, offhand, how to do that in PHP. How is the code you have not working? You should be able to see its effect in the access log.

Link to comment
Share on other sites

Ok.  Yes, you want a 301 redirect for the ones you don't want.  I don't know, offhand, how to do that in PHP.  How is the code you have not working?  You should be able to see its effect in the access log.

 

 

The exisiting 301 redirect works fine, however that is just to redirect from http://site.co.uk to http://www.site.co.uk

 

it doesn't redirect the other urls such as:

 

http://

 

www.mysite.co.uk/index.php?cPath=4

 

www.mysite.co.uk/index.php?cName=mtg-singles-4th-edition

 

www.mysite.co.uk/index.php?sort=4a&page=1&cName=mtg-singles-4th-edition&category=4th_Edition

Link to comment
Share on other sites

Then it should be a SMOP (Small Matter of Programming) to rewrite those URLs as well. All of the information is available to you in the server variables.

Link to comment
Share on other sites

See that $_SERVER['REQUEST_URI']? That's a server variable. There are a bunch of others that you can get at from PHP. See the PHP documentation. You want to grab the GET variables, remove the ones you don't want and reconstruct the URL to your liking.

 

Not all of us blond(e)s are dumb... :D

Link to comment
Share on other sites

See that $_SERVER['REQUEST_URI']?  That's a server variable.  There are a bunch of others that you can get at from PHP.  See the PHP documentation.  You want to grab the GET variables, remove the ones you don't want and reconstruct the URL to your liking.

 

Not all of us blond(e)s are dumb... :D

 

 

I looked at the oscommerce_documentation-20050228\table_of_contents.html

and couldn't find anything about variables.

Link to comment
Share on other sites

PHP documentation, not osC. But also look at the function tep_get_all_get_params in includes/general.php and search the osC sources to see how it is used.
Link to comment
Share on other sites

You can easily remove the sort and page values with tep_get_all_get_params, as it has an optional argument of values to remove. The trickier part is mapping the cPath numerical values to your new Search Engine Unfriendly URLs. How many of these do you have?

Link to comment
Share on other sites

PHP documentation, not osC.  But also look at the function tep_get_all_get_params in includes/general.php and search the osC sources to see how it is used.

 

I figure the answer likes somewhere in this code:

 

// Return table heading with sorting capabilities

function tep_create_sort_heading($sortby, $colnum, $heading) {

global $PHP_SELF;

 

$sort_prefix = '';

$sort_suffix = '';

 

if ($sortby) {

$sort_prefix = '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('page', 'info', 'sort')) . 'page=1&sort=' . $colnum . ($sortby == $colnum . 'a' ? 'd' : 'a')) . '" title="' . tep_output_string(TEXT_SORT_PRODUCTS . ($sortby == $colnum . 'd' || substr($sortby, 0, 1) != $colnum ? TEXT_ASCENDINGLY : TEXT_DESCENDINGLY) . TEXT_BY . $heading) . '" class="productListing-heading">' ;

$sort_suffix = (substr($sortby, 0, 1) == $colnum ? (substr($sortby, 1, 1) == 'a' ? '+' : '-') : '') . '</a>';

}

 

return $sort_prefix . $heading . $sort_suffix;

}

 

////

// Recursively go through the categories and retreive all parent categories IDs

// TABLES: categories

function tep_get_parent_categories(&$categories, $categories_id) {

$parent_categories_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$categories_id . "'");

while ($parent_categories = tep_db_fetch_array($parent_categories_query)) {

if ($parent_categories['parent_id'] == 0) return true;

$categories[sizeof($categories)] = $parent_categories['parent_id'];

if ($parent_categories['parent_id'] != $categories_id) {

tep_get_parent_categories($categories, $parent_categories['parent_id']);

}

}

}

 

////

// Construct a category path to the product

// TABLES: products_to_categories

function tep_get_product_path($products_id) {

$cPath = '';

 

$category_query = tep_db_query("select p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = '" . (int)$products_id . "' and p.products_status = '1' and p.products_id = p2c.products_id limit 1");

if (tep_db_num_rows($category_query)) {

$category = tep_db_fetch_array($category_query);

 

$categories = array();

tep_get_parent_categories($categories, $category['categories_id']);

 

$categories = array_reverse($categories);

 

$cPath = implode('_', $categories);

 

if (tep_not_null($cPath)) $cPath .= '_';

$cPath .= $category['categories_id'];

}

 

return $cPath;

}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...