This document will give an idea about how to create SEO friendly URLs in Opencart (version 2.x). Also will explain how to customize Opencart function to create SEO URLs of other than default pages (product page, category page etc.).
In this document will also explain how to 301 redirect URLs from old URL to new URL and URLs of 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 provides both humans and search engines an easy to understand indication of what the destination page will be about.
In Opencart there are setting which we have to manage from admin panel to activate SEO friendly URLs.
Following are the steps to enable:
- Open admin panel. Go to System > Setting. Then click on edit button corresponding to store from store listing page.
- In edit system setting page open ‘Server’ tab.
- In server tab select ‘Yes’ radio button corresponding to ‘Use SEO URL’ and then click on save button.
By doing this admin will be able to use SEO URLs
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 Option in 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 of ‘SEO Keyword’. Enter the SEO URL of the product and then save.
Refer below screen:
To save SEO URL of any category page go to Catalog > Categories in the admin panel. Click on the edit button corresponding to the category.
In edit category page click on ‘Data’ tab. In the Data tab there is an option of ‘SEO URL”. Enter SEO URL of category and then save.
Refer below screen:
To save SEO URL of any manufacturer page go to Catalog > Manufacturers in the admin panel. Click on the edit button corresponding to the manufacturer.
In edit manufacturer page there is an option of ‘SEO URL”. Enter SEO URL of the manufacturer and then save.
Refer below screen:
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.
In edit information page click on the ‘Data’ tab. In the Data tab, there is an option of ‘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 SEO URL of 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 controller file seo_url.php located at catalog/controller/startup. This file is called to find the SEO URL.
Note: SEO URL of each page are stores in ‘url_alias’ table in the database.
If customer hit any non-SEO URL then it calls seo_url.php file. In this file there is code which search SEO URL of non-seo url in ‘keyword’ column of url_alias table corresponding to ‘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 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 SEO URL for the page. For this first have to make entry of page in url_alias table.
If the admin has created ‘Sports’ landing page then he has to make an entry in 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 SEO URL for the blog page.
For this we have to add a condition in the controller file and make an entry in url_alias table corresponding to blog_id:
Refer below screenshot of seo_url.php file:
In Opencart if admin wants to create new SEO URLs and want to 301 (permanent redirection) or 302 (temporary redirection) redirect on new URL then the admin has to make following changes in 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 ‘termes and conditions’ page to the new URL.
If the admin wants to redirect any information page from old URL to new than he has to make an entry in url_alias table. Refer below screen shot:
- old keyword is: shop/terms-conditions.html
- new keyword: help/terms-conditions
Controller file changes: In seo_url.php controller admin has to handle condition if enter URL did not found in the keyword column.
The first system will search entered URL in keyword column. If not found then will search in old_keyword column. If found then will redirect to corresponding new URL.
Please refer below screenshot:
If the admin wants to redirect URL of deleted categories, products or any other pages on the home page than he can make changes in 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 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 home page
- Add a condition in seo_url.php controller to redirect from deleted category URL to new URL.
In this code first we have included 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 new URL else redirected to not found page.