Ecommerce software home
Shopping Cart Software Forum for Ecommerce Templates
 
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

Find us on Facebook Follow us on Twitter View our YouTube channel
Search our site
 All Forums
 Technical
 Tips and Tricks - HALL OF FAME
 Dynamic canonical link with URL rewrite
Author « Topic »  

Marshall
Ecommerce Template Guru

USA
1874 Posts

Posted - 03/08/2016 :  15:21:39  
If you use canonical links on your site so search engines know which URL you prefer, you need a dynamic canonical link for the product detail page. Unfortunately, with the web.config rewrite, using Request.ServerVariables("URL") is going to get you proddetail.asp?prod=, or at least that is what has happened to me. So here is a little code you can use in the <head> of the proddetail page.

<%
SET rs = Server.CreateObject("ADODB.RecordSet")
Set cnn=Server.CreateObject("ADODB.Connection")
cnn.open sDSN
SEOURL=""
prodid=replace(trim(request.querystring("prod")),detlinkspacechar," ")
IF prodid<>"" THEN
if usepnamefordetaillinks AND trim(request.querystring("prod"))<>"" then pidfield="pName" else pidfield="pID" 'ADDED 12-7-12
rs.Open "SELECT pName FROM products WHERE "&pidfield&"='"&Replace(prodid,"'","''")&"'",cnn,0,1
IF NOT rs.EOF THEN
SEOURL=strip_tags(rs("pName")&"")
END IF
rs.Close
END IF
SEOURL = Replace(SEOURL, " ", "-")
SEOURL = Replace(SEOURL, "'", "%27")
cnn.Close
set rs = Nothing
set cnn = Nothing
%>

<link rel="canonical" href="http://www.yourwebsite.com/<%=SEOURL%>">
or
<link rel="canonical" href="https://www.yourwebsite.com/<%=SEOURL%>">
if you are using SSL.
The two lines I put in red account for blank spaces and apostrophes in the product name. If you are not using a hyphen as the detail link separator, change the hyphen to whatever you use, e.g. underscore. If you have other special characters, such as an ampersand "&" add below the red lines
SEOURL = Replace(SEOURL, "&", "%26")
Hope this helps. Sorry I do not have a PHP version.

For more special characters and their ASCII equivalent, see http://www.w3schools.com/tags/ref_urlencode.asp


Marshall
CENLYT Productions - ms designs
Affordable Web Design
Cenlyt.com

From Our Heart
FromOurHeart.com
Gifts, Plush Stuffed Animals, Toy, Games, Puzzles, 3D Gift Cards, Nostalgic Signs, and Collectibles

Edited by - Marshall on 03/09/2016 01:13:30

ITZAP
Ecommerce Template Guru

Australia
1010 Posts

Posted - 03/29/2016 :  18:20:45  
This is PHP code to dynamically generate canonical url and Facebook og:url entries in the <head> of your proddetail.php page (and also any static proddetail pages) like so ...

<link rel="canonical" href="website url here" />
<meta property="og:url" content="website url here" />


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CRITICAL UPDATE Tuesday 15th December 2020

An issue has arisen where a site was identified as subject to an XSS (Cross Site Scripting) vulnerability. The culprit was identified to be this line of code:

return $url.($addprod&&@$_GET['prod']!=''?'?prod='.@$_GET['prod']:'');} ?>

If you have used this code then, on advice from Vince, please NOW REPLACE THAT LINE with the code highlighted in red below.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Using standard vanilla proddetail.php page URL's with no Search Engine Friendly RewriteRules, add this code into the <head> section.

<?php function getCurrentUrl($addprod) {
$url = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
$url.= '://' . $_SERVER['SERVER_NAME'];
$url.= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
$url.= $_SERVER['REQUEST_URI'];
$urlarray = explode("?", $url);
$url = $urlarray[0];
return str_replace('"','&quot;',strip_tags($url.($addprod&&@$_GET['prod']!=''?'?prod='.@$_GET['prod']:'')));} ?>
<link rel="canonical" href="<?php print getCurrentUrl(TRUE); ?>" />
<meta property="og:url" content="<?php echo getCurrentUrl(TRUE); ?>" />


If you do use the Search Engine Friendly URL's, then the canonical URL entries (last 2 lines) must be changed from TRUE to FALSE.

<link rel="canonical" href="<?php print getCurrentUrl(FALSE); ?>" />
<meta property="og:url" content="<?php echo getCurrentUrl(FALSE); ?>" />



Code tested and confirmed to work perfectly.

You can find a FREE download file containing HTML and PHP code snippets to generate Dynamic Page Title + Meta Description code here >>

Gary

Edited by - ITZAP on 12/14/2020 18:26:58

Mikelli
Ecommerce Template Guru

USA
1613 Posts

Pre-sales questions only
(More Details...)

Posted - 08/17/2018 :  14:38:18  
Hi Gary,
I'm not any way, shape, manner or form a coder. But if I wanted to keep spider bots from indexing both a http & https version of a page what would a rewrite look like if all I wanted indexed (thus no duplicate content) is the https://www.site.com version? I have put in place a 301 redirect, I just want to cover all bases.

"Using standard proddetail.php page URL's (with no Search Engine Friendly RewriteRules)"

Thanks,
Michael

Edited by - Mikelli on 08/17/2018 20:29:11

ITZAP
Ecommerce Template Guru

Australia
1010 Posts

Posted - 08/17/2018 :  20:36:24  
Hi Mikelli,
quote:
if I wanted to keep spider bots from indexing both a http & https version of a page what would a rewrite look like if all I wanted indexed (thus no duplicate content) is the https://www.site.com version? I have put in place a 301 redirect, I just want to cover all bases.

To convert an entire website to SSL secured https:// and re-submit the site to search engines for indexing, my procedure is:

(1) .htaccess

# rename file .htaccess
AddDefaultCharset UTF-8
<ifModule mod_headers.c>
# Enable HSTS, tells the browser to always use HTTPS
Header set Strict-Transport-Security "max-age=31536000" env=HTTPS
# Enable HTTP Keep-Alive to allow the same TCP connection to send and receive multiple HTTP requests, thus reducing the latency for subsequent requests
Header set Connection keep-alive
</ifModule>
# ======================================================
# URL Rewrite Rules
# ======================================================

<IfModule mod_rewrite.c>
Options -Multiviews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.site.com/$1 [R,L]
# REDIRECT to www
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.*)$ https://www.site.com/$1 [R=301,L]
# REDIRECT index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.site.com/$1 [R=301,L]
</IfModule>


(2) Ecommerce Templates Admin Panel

Open STORE ADMIN ==> Main Settings
Both Store URL: and HTTPS Store URL: must be set to https://www.site.com

(3) includes.php, site.css, website template, website page files

Make sure all links specify secure https://
You can Search and Find references to http:// and replace those with https://

And in PROD ADMIN, if any pages entered in there contain embedded images or links to other pages on your site, make sure all those links are https:// as well.

(4) Duplicate content: Consider whether you really do (or do not) want all your products.php pages indexed.

Personally, I don't consider their is much positive benefit in having the products.php pages indexed by search engines. The multi-page content there changes frequently and is merely duplicate information of that contained in more explicit detail on your proddetail.php pages. And if you have a manufacturers.php page, then there will be a stack more products.php pages generated; all the same duplicate stuff yet again, just ordered differently.

So you need to decide what you want indexed as a priority, and what you don't want indexed. What pages do you actually want visitors to land on? Instruct search bots accordingly.
EITHER products.php ... the summary pages.
OR proddetail.php ... the more detailed and beefy keyword rich product pages.
OR both ... in which case you leave it for Search Engines to decide which finally gets displayed in search results, and can indeed lead to duplicate content issues.

Make it simple for search bots to index your site properly, as you want. This is what I do on products.php

<meta name="robots" content="noindex, follow">

That instructs bots to follow links through to each of your proddetail.php pages, index that content (as a priority over the brief products.php content), and list those proddetail.php Titles and Short Descriptions in search results.

(5) robots.txt

User-agent: *
Sitemap: https://site.com/sitemap.xml


Upload new robots.txt to server.

(6) sitemap.xml

Generate a new "sitemap.xml" file containing all absolute https:// website page links.
Upload to server.

(7) Google Webmaster Tools account.

ADD A PROPERTY (new website address to index) for:
https://site.com
AND
https://www.site.com

Why do you need both? Because Google says so.
Verify ownership of both.

Next, under Site Settings, Confirm your preferred domain. You will get an email message asking you to do this. In your case, you will want to select:
> Display URLs as www.site.com

Next, under Crawl => Sitemaps, submit your new "sitemap.xml" file for indexing.

From then on, the only Google Webmaster Tools account you need to look at is the: https://www.site.com

(8) Bing Webmaster Tools account.

Much the same deal.

(9) Google Analytics, Statcounter, Google+, YouTube, Facebook, Twitter, Pinterest, whatever else contains links to your website.

Change website URL in all those accounts to https://


It only takes a week or two for search engines to re-index your https:// website. A little short term pain for long term gain. Trust that helps and makes sense.

Gary

Edited by - ITZAP on 08/17/2018 21:31:50

Mikelli
Ecommerce Template Guru

USA
1613 Posts

Pre-sales questions only
(More Details...)

Posted - 08/17/2018 :  21:04:10  
Gary, you are awesome and your procedure for conversion is very logical! I'm heading to bed, it's after 11pm here in Texas . . . so tomorrow I'll plug and play and see what happens.
Thank you so much, you are the man!

Many Thanks

Michael

ITZAP
Ecommerce Template Guru

Australia
1010 Posts

Posted - 08/17/2018 :  23:04:44  
One more thing to consider. Why www ?
Now is the perfect opportunity to switch to the shorter https://site.com
In which case you would use the .htaccess 301 redirect entries here >


Gary

Mikelli
Ecommerce Template Guru

USA
1613 Posts

Pre-sales questions only
(More Details...)

Posted - 09/03/2018 :  20:33:17  
I would add one more tip: change your stores emails to https so that any emails with logos will still display.

Great stuff Gary!!

Michael

tgorski
Ecommerce Template Expert

USA
905 Posts

Posted - 10/13/2022 :  06:58:30  
I need some help with the canonical code for php that ITZAP has produced.
I would like the link that is produced for the page, to have + signs to replace the spaces that it is currently generating.
Currently all of the proddetail pages display like this on a browser... proddetail.php?prod=Beef+Jerky+1lb
If I view source code on the page it says the canonical link is this...
<link rel="canonical" href="https://XXXXXXXXX.com/proddetail.php?prod=Beef Jerky 1lb" /> <--SEE THE SPACES?
<meta property="og:url" content="https://XXXXXXXXX.com/proddetail.php?prod=Beef Jerky 1lb" />
Is this correct because I am consistently getting an error from Google Search Console..."the following issue: 'Duplicate without user-selected canonical'."

How can I fix this Search Console error?


Tim Gorski

tgorski
Ecommerce Template Expert

USA
905 Posts

Posted - 10/14/2022 :  08:29:31  
I think I figured it out. The + does not work in the detailspachar field in the includes... I had to change it to an underscore.

Tim Gorski

Vince
Administrator

42466 Posts

Posted - 10/14/2022 :  09:19:30  
Hi Tim
That sounds likely as the "+" symbol in a URL is taken as a space character. Glad you got it sorted anyway.

Vince

Click Here for Shopping Cart Software
Click Here to sign up for our newsletter
Click Here for the latest updater
  « Topic »  
Jump To:
Shopping Cart Software Forum for Ecommerce Templates © 2002-2022 ecommercetemplates.com
This page was generated in 0.03 seconds. Snitz Forums 2000