Advertisement
  1. Code
  2. OpenCart
Code

How to Rewrite Custom URLs in OpenCart

by
Difficulty:IntermediateLength:ShortLanguages:

In the course of this tutorial, we'll go through the details of custom SEO-friendly URLs. It's an important aspect you should look at while developing custom modules, and it affects the overall search engine rankings as well.

We'll use the latest version of OpenCart, so make sure that you've installed that to follow the code.

What We Are Going to Do—in a Nutshell

Here's a summary of the steps we'll need to take to achieve custom URLs in OpenCart:

  • First, we will need to understand how it works in the core.
  • We'll go through the changes required in the core files.
  • We'll take a quick look at the SQL queries to insert our custom URL mappings.
  • We'll cover how to use built-in rewrite functions.

So, that's the quick glance of what's coming up next. Let's move on to the first topic.

The URL Mappings in a Database

First of all, it's important to understand how SEO URLs work in the core of OpenCart.

Go ahead and explore the entries in the "url_alias" MySQL table using phpMyAdmin or something similar. You'll see mappings like this.

There are two important columns to note here. The first one is the query column which stores the actual path, and the other is keyword, which stores the SEO alias for that URL.

As you can see, there are mappings for different entities like product, category, information, and manufacturer. Whenever any entity is saved in the back-end of OpenCart, an associated entry is added to this table.

In the front-end, whenever the user accesses the URL, OpenCart finds the related mapping from the "url_alias" table. In this way, the actual entity is mapped to the SEO-friendly URL.

Go ahead and open the file catalog/controller/common/seo_url.php, and let's explore the following snippet from the index method.

As you can see, we are fetching the associated entry from the "url_alias" table. After that, the query parameter is parsed and an associated internal path is returned.

So, this is the way it works in the core. Unfortunately, the setup only works for core URLs—for custom URLs we need to alter the core code. That's the recipe of our next section.

The Core File Changes

Go ahead and open the file catalog/controller/common/seo_url.php, and replace the rewrite method with the following one.

The rewrite method is used to convert an internal URL into an SEO-friendly URL. But it does this only for internal URLs. Hence, we need to add custom code to make it work for our custom modules as well. In our code changes, we have provided the last else case which loads the mapping for our custom module. We have not added our custom mapping yet, so let's do it in our next section.

Although we have modified the core file directly, it's just for the sake of simplicity. You should use OCMOD to alter the core file without actually modifying it.

Add MySQL Entries

In this section, we will add an SEO URL mapping for our custom module. Again, it's a plain SQL for example purposes—you can achieve that using module install scripts.

Go ahead and run it in your OpenCart database.

In the next and final section, we will see how to use the helper function to produce SEO-friendly links.

How It Works

Go ahead and create a new file catalog/controller/custom/custom.php with the following contents.

Now, in the front-end, open the URL http://www.yourstore.com/index.php?route=custom/custom.

Yeah, it's a plain white screen just with one link, and that is what we intended. The important thing to note here is the URL of that link— it's now SEO-friendly! Click on that and it will load the same page, as we have added the mapping for that in the "url_alias" table.

So, that's the whole concept demonstrated in a simple way. You could extend it and make a model to insert SEO-friendly links for your custom module.

Conclusion

Today, we have discussed an important topic in OpenCart—custom SEO-friendly URLs. We took a very simple approach to explain it, and I hope that it was useful for you.

If you're looking for additional OpenCart tools, utilities, extensions, and so on that you can leverage in your own projects or for your own education, don't forget to see what we have available in the marketplace.

Feel free to post your queries and suggestions using the feed below.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.