Randem Systems Support Board

OpenCart => OpenCart MODS => Topic started by: Randem on February 12, 2016, 09:15:18 PM

Title: Getting SEO URL's & Keywords To Work
Post by: Randem on February 12, 2016, 09:15:18 PM
There is much incomplete information on how to get SEO keywords to work. We will attempt to clear much of it up and get your SEO URL's to work in a few minutes.
The first thing is to set SEO keywords in all your Products / Categories / Manufacturers. This can be a daunting task if you already have a large amount of products. We will help you get thru this by executing a few SQL queries in phpMyAdmin.

First we want to change the INDEXES to UNIQUE INDEXES to force all SEO keywords to be unique. For best results empty the url_alias table first. Then we add all the newly created SEO keywords for Products / Categories / Manufacturers. If you have duplicates in your Categories / Products / Manufacturers they will not be added here. You will need to go to the Category / Product / Manufacturer in the admin panel to change the item to make them unique or to modify any item formatted in a manner that you do not like. If there was a duplicate it will not be present in the SEO field in the Product / Category / Manufacturers.

Changing these indexes will create an error condition when the category or product is saved and you will get the error message 'SEO keyword already in use!' and will force you honest in your SEO keywords. In OpenCart before this change you can enter duplicate SEO keywords which is a big No-No.



ALTER TABLE `oc_url_alias` DROP INDEX `keyword` ,
ADD UNIQUE `keyword` ( `keyword` )

ALTER TABLE `oc_url_alias` DROP INDEX `query` ,
ADD INDEX `query` ( `query` )

INSERT IGNORE into `oc_url_alias` (`query`, `keyword`) (SELECT concat('product_id=',`product_id`) as query, replace(replace(`model`,' - ','-'),' ','-') as keyword FROM `oc_product`)

INSERT IGNORE into `oc_url_alias` (`query`, `keyword`) (SELECT concat('category_id=',`category_id`) as query, replace(replace(`name`,' - ','-'),' ','-') as keyword FROM `oc_category_description`)

INSERT IGNORE into `oc_url_alias` (`query`, `keyword`) (SELECT concat('manufacturer_id=',`manufacturer_id`) as query, replace(replace(`name`,' - ','-'),' ','-') as keyword FROM `oc_manufacturer`)



What these queries do is take your Product / Category / Manufacturer name and use it for your SEO keyword. So you should have unique names in your tables. It basically takes the name then replaces the single spaces with a single dash then inserts the results into the url_alias table where the SEO keywords are stored. Once this is done you can turn on 'use SEO URLs'  in the store settings, but we are not quite done yet...

You will need to setup your .htaccess file to use SEO Keywords. In your OpenCart folder you have a file named htaccess.txt, rename this file to .htaccess. You can also use our file directly or just add the missing portions that are in our file to your .htaccess file if you already have one. The important part of this that I have not seen explained very well is that one key change to the file is needed if your store is not in the root folder of your domain. The line you must change, if your store is in a sub-folder; let call the folder /store/; so your store is accesses by https://mydomain.com/store/. You have to change the line that reads RewriteBase /  to become RewriteBase /store/ or else it will never work. If your store is in the root folder leave the file as it is.
Once you have this file in place your OpenCart will now use SEO keywords in the URL's. I have heard that you need this file in the root for your store, in the admin folder and the catalog folder. I don't know for sure that it is needed in all those locations but better safe than sorry I guess.



Options +FollowSymlinks

# Prevent Directoy listing
Options -Indexes

# Prevent Direct Access to files
<FilesMatch "\.(tpl|ini|log)">
Order deny,allow
Deny from all
</FilesMatch>

# SEO URL Settings
RewriteEngine On
#RewriteCond %{HTTPS} on

#SSL Port
RewriteCond %{SERVER_PORT} !=443

# Remove www. from any domain query
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,L]

# All broswer request get redirected to HTTPS
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

# If your opencart installation does not run on the main web folder but runs in /shop/ make sure you change 
# RewriteBase /  to become RewriteBase /shop/

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]