This document will give an idea about how to create SEO Friendly URLs in Opencart (version 2.x). Also will explain how to customize the Opencart function to create SEO URLs other than default pages (product page, category page etc.).
This document will also explain how to 301 redirect URLs from old URL to new URL and URLs deleted.
The main purpose of this document is to learn about creating SEO Friendly URLs in Opencart (version 2.x) and URL redirections to maintain our SEO ranking on Google.
SEO Friendly URLs are well-crafted URLs which provide both humans and search engines with an easy-to-understand indication of what the destination page will be about.
In Opencart there are setting which we have to manage from the admin panel to activate SEO Friendly URLs.
- Open admin panel. Go to System > Setting. Then click on the edit button corresponding to the store from the store listing page.
- In the edit system setting page open the ‘Server’ tab.
- In the server, the tab selects the ‘Yes’ radio button corresponding to ‘Use SEO URL’ and then clicks on the save button.
By doing this admin will be able to use SEO URLs.
Recommended Read: Top 10 Opencart Extensions to Boost Customer Experience
In Opencart there is a default option to create SEO-friendly URLs for the following page:
- Product page
- Category page
- Manufacturer page
- Information page
For all these above pages there is an option on their respective edit page to enter SEO URL.
To save the SEO URL of any product page go to Catalog > Products in the admin panel. Click on the edit button corresponding to the product name.
In the edit product page click on the ‘Data’ tab. In the Data tab, there is an option for ‘SEO Keyword’. Enter the SEO URL of the product and then save.
Refer below screen:
To save the SEO URL of any category page go to Catalog > Categories in the admin panel. Click on the edit button corresponding to the category.
In the edit category page click on the ‘Data’ tab. In the Data tab, there is an option for ‘SEO URL”. Enter SEO URL of category and then save.
Refer below screen:
To save the SEO URL of any manufacturer page go to Catalog > Manufacturers in the admin panel. Click on the edit button corresponding to the manufacturer.
On the edit manufacturer page, there is an option for ‘SEO URL”. Enter the SEO URL of the manufacturer and then save.
Refer below screen:
Recommended Read: What are the merits and demerits of using an OpenCart platform?
To save the SEO URL of any information page go to Catalog > Information in the admin panel. Click on the edit button corresponding to the information page.
On the edit, the information page clicks on the ‘Data’ tab. In the Data tab, there is an option for ‘SEO URL”. Enter the SEO URL of the information page and then save.
Refer below screen:
Note: Admin can also save SEO URL while adding a new product, category, manufacturer and information page.
If the admin wants to create an SEO URL for any custom pages then he/she has to make changes accordingly (explained below in the same document).
In Opencart (version 2.x) when the user hit any URL (SEO or non-SEO) it calls the controller file seo_url.php located at catalogue/controller/startup. This file is called to find the SEO URL.
Note: The SEO URL of each page is stored in the ‘url_alias’ table in the database.
If the customer hit any non-SEO URL then it calls the seo_url.php file. In this file, there is code which searches the SEO URL of the Non-SEO URL in the ‘keyword’ column of the url_alias table corresponding to the ‘query’ column
url_alias table is used to save SEO URLs of all pages. There are the following 3 columns in the table by default:
- url_alias_id: Primary key of the table.
- Query: contains the id of different pages like product_id, category_id, and other pages.
- Keyword: contains SEO URL keyword of the corresponding page
If the admin has created landing pages and wants to serve an SEO URL for the page. For this first have to make an entry of the page in the url_alias table.
If the admin has created the ‘Sports’ landing page then he has to make an entry in the url_alias table as:
- In query column: sport_id=1
- In keyword column: sports
Admin also has to make changes in controller file seo_url.php for sport_id. Admin has to add a condition for sport_id same as product_id and category_id.
Now if admin enter URL “host/index.php?route=product/landing_page&sport_id=1” then code will search seo keyword in url_alias table corresponding to sport_id=1 and then serve URL ‘host/sports’.
In this example, we will create an SEO URL for the blog page.
For this we have to add a condition in the controller file and make an entry in the url_alias table corresponding to blog_id:
Refer below screenshot seo_url.php file:
Recommended Read: How can you change the currency format in an OpenCart platform?
In Opencart if the admin wants to create new SEO URLs and wants 301 (permanent redirection) or 302 (temporary redirection) redirect on a new URL then the admin has to make the following changes in the controller file and url_alias table.
- Create a new column old_keyword in url_alias table of type ‘varchar’.
- Handel condition in the controller file to search in old_keyword column if user search for old URL.
Example: To redirect from the old URL of the ‘termes and conditions’ page to the new URL.
If the admin wants to redirect any information page from the old URL to the new then he has to make an entry in the url_alias table. Refer to the below screenshot:
- the old keyword is: shop/terms-conditions.html
- new keyword: help/terms-conditions
Controller file changes: In seo_url.php controller admin has to handle conditions if enter URL did not found in the keyword column.
The first system will search entered URL in the keyword column. If not found then will search in the old_keyword column. If found then will redirect to the corresponding new URL.
Please refer to the below screenshot:
If the admin wants to redirect the URL of deleted categories, products or any other pages on the home page then he can make changes in the controller file accordingly.
Example: Here we will 301 redirect URLs of deleted categories
Steps to redirect URL of deleted categories:
- Create a file and define an array with the key as deleted category URL and value as URL to which admin wants to redirect. Refer to the below screenshot for creating an array of deleted categories.
Note: Here we have defined blank value because we want to redirect deleted categories URLs to the home page
- Add a condition in the seo_url.php controller to redirect from deleted category URL to the new URL.
In this code first, we have included a file containing URLs of deleted categories and then checked if the entered URL is in the defined array or not. If yes then redirected to a new URL else redirected to the not found page.
A variety of OpenCart modules and extensions, both free and paid, are available from Knowband to boost your store’s operation. To know in detail about the OpenCart platforms, modules available or any functionality related to the platform feel free to drop us an email at support@Knowband.com.