Adding a canonical link, or redirect, to index.php

Ask the community for help and support.
Post Reply
cin
Posts: 12
Joined: Wed Apr 28, 2021 6:48 pm
Has thanked: 2 times

Adding a canonical link, or redirect, to index.php

Post by cin »

I know that my post is about 2.3.4 BS GOLD, not Phoenix, but the new people at oscommerce.com suggested that their forum is no longer the place for support of previous versions... so you are my only hope.

I'm trying to add a canonical link in mysite.com/myosc/index.php that would point to mysite.com/myosc/

I'm trying to prevent Google from seeing those as 2 different pages. I've already removed index.php from my internal links, I just need to add a canonical link or redirect to make it clear to Google.

If I'm not mistaken, index.php is used for more than just the front page of the store? Like it's used to list products in categories and manufacturers, etc? (I have Ultimate SEO installed so that part may be obfuscated in the URLs, thus my hesitation)

What's the best way to either add a canonical link to my index.php only when appropriate?

Or am I better off having a redirect in my htaccess? If so, how should it make sure only to redirect when it's the actual front page of the store, and not a category/manufacturer/etc?

Thank you kindly.

p.s. Yes, ideally I'd upgrade to Phoenix and use the new Header Tags Seo. Unfortunately it's not an option right this moment, but I'm looking into it.

Tags:
ecartz
Lead Developer
Lead Developer
Posts: 2637
Joined: Tue Nov 05, 2019 6:02 pm
Has thanked: 4 times
Been thanked: 181 times

Re: Adding a canonical link, or redirect, to index.php

Post by ecartz »

As best I can see, this version of the header tag module should work: https://github.com/CE-PhoenixCart/Phoen ... onical.php
14Steve14
VIP Member
VIP Member
Posts: 549
Joined: Fri Oct 25, 2019 7:01 pm
Has thanked: 8 times
Been thanked: 49 times

Re: Adding a canonical link, or redirect, to index.php

Post by 14Steve14 »

Reference or link to service providers not in the list of Certified Developers deleted by Admin
cin
Posts: 12
Joined: Wed Apr 28, 2021 6:48 pm
Has thanked: 2 times

Re: Adding a canonical link, or redirect, to index.php

Post by cin »

ecartz wrote: Thu May 13, 2021 3:16 am As best I can see, this version of the header tag module should work: https://github.com/CE-PhoenixCart/Phoen ... onical.php
Unfortunately that version breaks my site.

Just the same, by comparing my version with the Phoenix version, I was able to feel confident enough to modify my ht_canonical for my purpose.

My execute function looked like this:

Code: Select all

function execute() {
      global $PHP_SELF, $HTTP_GET_VARS, $cPath, $oscTemplate;

      if (basename($PHP_SELF) == FILENAME_PRODUCT_INFO) {
        $oscTemplate->addBlock('<link rel="canonical" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . (int)$HTTP_GET_VARS['products_id'], 'NONSSL', false) . '" />' . "\n", $this->group);
      } elseif (basename($PHP_SELF) == FILENAME_DEFAULT) {
        if (isset($cPath) && tep_not_null($cPath)) {
          $oscTemplate->addBlock('<link rel="canonical" href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath, 'NONSSL', false) . '" />' . "\n", $this->group);
        } elseif (isset($HTTP_GET_VARS['manufacturers_id']) && tep_not_null($HTTP_GET_VARS['manufacturers_id'])) {
          $oscTemplate->addBlock('<link rel="canonical" href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . (int)$HTTP_GET_VARS['manufacturers_id'], 'NONSSL', false) . '" />' . "\n", $this->group);
        }
      }
    }
Now it looks like this:

Code: Select all

function execute() {
      global $PHP_SELF, $HTTP_GET_VARS, $cPath, $oscTemplate;

      if (basename($PHP_SELF) == FILENAME_PRODUCT_INFO) {
        $oscTemplate->addBlock('<link rel="canonical" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . (int)$HTTP_GET_VARS['products_id'], 'NONSSL', false) . '" />' . "\n", $this->group);
      } elseif (basename($PHP_SELF) == FILENAME_DEFAULT) {
        if (isset($cPath) && tep_not_null($cPath)) {
          $oscTemplate->addBlock('<link rel="canonical" href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath, 'NONSSL', false) . '" />' . "\n", $this->group);
        } elseif (isset($HTTP_GET_VARS['manufacturers_id']) && tep_not_null($HTTP_GET_VARS['manufacturers_id'])) {
          $oscTemplate->addBlock('<link rel="canonical" href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . (int)$HTTP_GET_VARS['manufacturers_id'], 'NONSSL', false) . '" />' . "\n", $this->group);
        } elseif ($cPath == '') {
		  $oscTemplate->addBlock('<link rel="canonical" href="' . tep_href_link(FILENAME_DEFAULT, '', 'NONSSL', false) . '" />' . "\n", $this->group);
		}
      }
    }
Just adding a canonical to my index. Looks right, right?
nehakakar
Posts: 3
Joined: Tue Jul 04, 2023 12:40 pm
Has thanked: 1 time

Re: Adding a canonical link, or redirect, to index.php

Post by nehakakar »

You can simply edit your index page and add below line

Code: Select all

<link rel="canonical" href="https://www.mysite.com/myosc/" />
or
You can simply edit your .htaccess with below script as i did it.

Code: Select all

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /myosc/index\.php [NC]
RewriteRule ^ /myosc/ [R=301,L]
Replace /myosc/ with your URL path..
This code will redirect any request to index.php to the root URL of your site using a 301 redirect.
This is beneficial for SEO Ranking search engines
Once done you can verify your URL manually by using browser developer tools to inspect network requests and redirects or use an online tool
Post Reply