You are here:  » Change rewrite for product page


Change rewrite for product page

Submitted by wilkins on Sat, 2008-03-29 08:31 in

Hi david

I have been doing some tests for one of our sites and had found a marked difference in search engine ranking between 2 identical pages where one is at root level and one is at the 1st level down.

So I have decided to make one of our sites have all the product pages rewritten as domain.com/product.html. I have done the htaccess without any problem. In the searchresults.php template the following peice of code fetches the rewritten url of the product page, at present it shows it as domain.com/product/product.html, can this be changed to show domain.com/product.html.

<?php
 
print $product["productHREF"];
?>

Thamks

Brent

Submitted by support on Sat, 2008-03-29 10:59

Hi Brent,

Sure - the productHREF value is set in search.php, on line 166 of the distribution...

        $searchresults["products"][$k]["productHREF"] = "product/".tapestry_hyphenate($product["name"]).".html";
        if ($rewrite) $searchresults["products"][$k]["productHREF"] = "../../".$searchresults["products"][$k]["productHREF"];

...I would replace both lines with just:

$searchresults["products"][$k]["productHREF"] = $config_baseHREF.tapestry_hyphenate($product["name"]).".html";

Cheers,
David.

Submitted by mally on Tue, 2008-05-06 23:05

Hello David

Could you exaplin how to do this, which files to change and what to change in the .htaccess ?

thanks

Mally

Submitted by support on Wed, 2008-05-07 12:54

Hi Mally,

As long as you don't have any other .html files in your top level directory, this should just be a case of modifying the product rewrite in .htaccess from:

RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

...to:

RewriteRule ^(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

From a code point of view, the modification above in this thread is required in search.php in order to link to the correct location (instead of the /products/ folder).

A similar modification would then be required in products.php to create the correct links for the related products. Very similar to the above modification, replace the following code (starting at line 87 in the distribution):

          $searchresults["products"][$k]["productHREF"] = "product/".tapestry_hyphenate($related["name"]).".html";
          if ($rewrite) $searchresults["products"][$k]["productHREF"] = "../".$searchresults["products"][$k]["productHREF"];

with just...

          $searchresults["products"][$k]["productHREF"] = $config_baseHREF.tapestry_hyphenate($related["name"]).".html";

Finally, in reviews.php (if you are using the reviews feature), the link back to the product page needs to be changed from the following code (line 47):

$rows[$k]["productHREF"] = "../product/".tapestry_hyphenate($row["name"]).".html";

to...

$rows[$k]["productHREF"] = $config_baseHREF.tapestry_hyphenate($row["name"]).".html";

Cheers,
David.

Submitted by Diana on Thu, 2008-05-08 01:52

Hi David.

Submitted by Diana on Thu, 2008-05-08 02:52

Should have mentioned the 404 error is because it is looking for:

/review/product-name.html instead of /PTsubdomain/review/product-name.html

The config file is correct.

$config_baseHREF = "/PTsubdomain/";

Thanks, Diana

Submitted by support on Thu, 2008-05-08 07:30

Hi Diana,

Yes - I see the problem... In products.php, change line 24:

$rows[$k]["reviewHREF"] = "../review/".tapestry_hyphenate($row["name"]).".html";

as follows:

$rows[$k]["reviewHREF"] = $config_baseHREF."review/".tapestry_hyphenate($row["name"]).".html";

The orignal version using "../" is expecting to be in the virtual (rewritten) /product/ directory, which of course no longer exists. Using $config_baseHREF instead will sort this.

Cheers,
David.

Submitted by Diana on Thu, 2008-05-08 17:09

Better, but still not working. No longer get 404 error.
address bar shows:http://www.mysite.com/ptsubdomain/review/productname.html

Page is blank where the product review should be followed by the banner which says search results for reviewproductname (product not found)

The related products are underneath.

I'll wait to hear from you as I don't want to muck anything up.
Thanks David.

Submitted by support on Thu, 2008-05-08 18:39

Hi Diana,

It sounds like the new rewrite rule for top level products is overriding the additional rules.

The way to fix this is to move the product rewrite rule to the end of your .htacess - that will make sure that it doesn't interfere with anything else that ends in .html!

Cheers,
David.

Submitted by Diana on Fri, 2008-05-09 02:06

That fixed it. Sometimes the pages load slowly, but I think it is a server issue.
Your the best, thanks. Diana

Submitted by Diana on Fri, 2008-05-09 21:00

Found one last glitch. Featured products links still have /product/ in the address and so they are not found. What would be the best fix to correct that? Thanks, Diana

Submitted by Diana on Sat, 2008-05-10 04:00

Sorry, and also merchant links in banner located under the product description.
Code is in html/prices.php
<td><u><a href='<?php print $product["merchantHREF"]; ?>'><?php print $product["merchant"]; ?></a></u></td>
Currently goes to:
merchant/merchant_name/ instead of
PTsubdomain/merchant/merchant_name/

Hopefully that is the last one. Diana

Submitted by support on Sat, 2008-05-10 08:00

Hi Diana,

The featured product links are generated by the following code in index.php (line 61 in the distribution):

$featured["products"][$k]["productHREF"] = "product/".tapestry_hyphenate($product["name"]).".html";

...this would need to be changed to:

$featured["products"][$k]["productHREF"] = $config_baseHREF.tapestry_hyphenate($product["name"]).".html";

For the merchant links in the prices table, i'd fix this by using $config_baseHREF to avoid ambiguity... so rather than a modification in the code you posted (from html/prices.php) look for the following code in products.php (line 22 in the distribution):

          $rows[$k]["merchantHREF"] = "../merchant/".tapestry_hyphenate($row["merchant"])."/";

...and change this to:

          $rows[$k]["merchantHREF"] = $config_baseHREF."merchant/".tapestry_hyphenate($row["merchant"])."/";

(I should have spotted that when posting the fix for the review link above!! sorry...!)

Cheers,
David.

Submitted by Diana on Sun, 2008-05-11 04:44

Everything seems to work beautifully. Thanks David for all the time you take to help the people that use your product. Your customer service is unmatched anywhere.
Diana

Submitted by paddyman on Fri, 2008-05-23 10:46

Hi All,

Just wondering if anyone that has implemented this mod has seen a marked improvement in rankings?

Thanks

Adrian

Submitted by mally on Sat, 2008-05-24 03:29

Hello David

I tried modifying the sitemap.php however it is producing an extra / after the domain name.

Can you have a look at the standard sitemap.php file to see what needs changing to implement this?

thanks

Mally

Submitted by support on Sat, 2008-05-24 08:08

Hi Mally,

The change would be on line 32, currently:

$sitemapHREF = "product/".urlencode(str_replace(" ","-",$row["name"])).".html";

...which would need to be modified as follows:

$sitemapHREF = urlencode(str_replace(" ","-",$row["name"])).".html";

(if perhaps you had only deleted the word "product" but left the "/" that would explain the extra "/" after the domain name...)

Cheers,
David.

Submitted by mally on Tue, 2008-05-27 23:22

Hi David, thanks that works

Would you be able to give details about how to make http://www.magazinesubscription.co.uk/sitemapquery.php rewrite search engine friendly?

My code is

<?php
  require("includes/common.php");
  header("Content-Type: text/xml");
  print "<?xml version='1.0' encoding='UTF-8'?>";
  print "<urlset xmlns='http://www.google.com/schemas/sitemap/0.84' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd'>";
  $sql = "SELECT * FROM querylog ORDER BY id LIMIT 50000";
  if (database_querySelect($sql,$rows))
  {
    $sitemapBaseHREF = "http://".$_SERVER["HTTP_HOST"].$config_baseHREF;
    foreach($rows as $row)
    {
      print "<url>";
      $sitemapHREF = "search.php?q=".urlencode($row["query"]);
      print "<loc>".$sitemapBaseHREF.$sitemapHREF."</loc>";
      print "</url>";
    }
  }
  print "</urlset>";
?>

thanks alot

Cheers

Mally

Submitted by support on Wed, 2008-05-28 08:33

Hi Mally,

Do you already have a mod to make rewritten URLs for search results? It's quite straight forward - all you need to do is add the following to your .htaccess:

RewriteRule ^search/(.*)/$ search.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

This will mean you can submit search queries to your site using:

http://www.example.com/search/Your-Query/

You can still keep your original search working as normal (non rewritten) even after incorporating this modification, and I would recommend keeping the rewritten version for the sitemap queries only. Then, to modify your sitemapquery.php accordingly, simply change:

  $sitemapHREF = "search.php?q=".urlencode($row["query"]);

...to:

  $sitemapHREF = "search/".tapestry_hyphenate($row["query"])."/";

Cheers,
David.

Submitted by mally on Fri, 2008-06-06 15:13

Hello David.

Submitted by support on Fri, 2008-06-06 19:13

Hi Mally,

It's possible if you have allowed certain characters (stripped by the default normalisation) that they are breaking the XML. One way around this is to use a function similar to htmlentities but to make content safe for inserting into an XML document.

First, add the following function to the script:

  function xmlentities($text)
  {
    $search = array('&','<','>','"','\'');
    $replace = array('&amp;','&lt;','&gt;','&quot;','&apos;');
    $text = str_replace($search,$replace,$text);
    return $text;
  }

(either right at the beginning, or at the end, just after/before the PHP tags)

Then, modify the following line:

      print "<loc>".$sitemapBaseHREF.$sitemapHREF."</loc>";

...as follows:

      print "<loc>".xmlentities($sitemapBaseHREF.$sitemapHREF)."</loc>";

Cheers,
David.

Submitted by mally on Fri, 2008-06-06 23:20

Hi David

Its come back with the following error

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
Only one top level element is allowed in an XML document. Error processing resource 'http://www.magazinesubscription.co.uk/...
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home/magsub/publ...

Submitted by philstone on Mon, 2008-06-09 08:37

Hi Dave

Hope your keeping well

Was wondering if you could look at something for me, when i look up indexed details of my site on google and say click on a link eg http://www.buy24-7.net/product/SAMSUNG-LE40F86BDX.html is there anyway it could forward to http://www.buy24-7.net/SAMSUNG-LE40F86BDX.html instead of creating an inacurate search results page?

Regards

phil

Submitted by support on Mon, 2008-06-09 10:40

Hi Phil,

That should be reasonably straight forward - but only do this if you are not planning to use /product/ any longer on this site as you would basically need to redirect everything at /product/Product-Name.html to /Product-Name.html. This is easy to do. It looks like you currently still have in your .htaccess the following original /product/ rule:

RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

Simply change this as follows to redirect to the new product pages:

RewriteRule ^product/(.*).html$ $1.html [L,R=301]

Make sure this goes at the top of the rule set (i.e. before the new product rule). It will issue a 301 (moved permanently) redirect, so search engines should update their index automatically as well.

Cheers,
David.

Submitted by philstone on Mon, 2008-06-09 11:35

Thanks Dave, Works Perfect!!!