/ OpenCart

Create SEO-Friendly URLs for OpenCart pages

Hi everyone, in this blog post I will be addressing a very common issue with OpenCart which has bugged me for a while but I’ve only recently looked into it. OpenCart has a very handy SEO-Friendly function which allows you to specify the URL on product, information and category pages but it has no option for the core pages within OpenCart.

For example, the standard homepage URL in OpenCart is:

[DOMAIN NAME].com/index.php?route=common/home

Hardly useful, although a common workaround would be to just use the domain name as the homelink without the filename. However, the contact page is at:

[DOMAIN NAME].com/index.php?route=information/contact

This does nothing for user experience. So in this blog I will be showing you how to change these URLs to something easier for users(to something like [DOMAIN NAME].com/contact, each URL change is a two step process as shown below:

  1. Edit the .htaccess file to create a unique rule for the page URL
  2. Change which URL OpenCart redirects users to when using the site

Preparation for SEO-Friendly URLs in OpenCart Pages

OK, so the first thing to do is download the .htaccess file which is in the root folder of your OpenCart store and open it up in a text editor such as Notepad++. When you open it up you will see something like the default .htaccess code as shown below:


# 2. In your opencart directory rename htaccess.txt to .htaccess.

# For any support issues please visit: http://www.opencart.com

Options +FollowSymlinks

# Prevent Directoy listing 
Options -Indexes

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

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base  [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]

### Additional Settings that may need to be enabled for some servers 
### Uncomment the commands by removing the # sign in front of it.
### If you get an "Internal Server Error 500" after enabling any of the following settings, restore the # as this means your host doesn't allow that.

# 1. If your cart only allows you to add one item at a time, it is possible register_globals is on. This may work to disable it:
# php_flag register_globals off

# 2. If your cart has magic quotes enabled, This may work to disable it:
# php_flag magic_quotes_gpc Off

# 3. Set max upload file size. Most hosts will limit this and not allow it to be overridden but you can try
# php_value upload_max_filesize 999M

# 4. set max post size. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value post_max_size 999M

# 5. set max time script can take. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_execution_time 200

# 6. set max time for input to be recieved. Uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_input_time 200

Your store will also need to have SEO-Friendly URLs turned on in the settings menu which you can reach through the admin panel.

Changing the 'Contact us' page URL in OpenCart

Right, so in this example we will be changing the OpenCart contact us page to something more obvious to users as shown below:

[DOMAIN NAME].com/index.php?route=information/contact

will change to

[DOMAIN NAME].com/contact

to do this we will build an extra ReWrite rule and add it to the .htaccess file. I’ll go through what this new rule does exactly here:

RewriteRule ^contact/?$ index.php?route=information/contact [L] #Contact Page

  • 1st Section – This is the part where we declare it is a rewrite rule and what the rule should look for. The ^ symbol marks the start of the search string and the $ marks the end of the string. I have added the /? to allow the end slash or not meaning users can write either …/contact or ../contact/.
  • 2nd Section – This is the part which tells the .htaccess file what to change the URL to if it finds the string highlighted in the Red Section. This is the default contact page URL as I’m sure you’ve noticed :)
  • 3rd Section – This is a simple part which tells the .htaccess file that it should stop processing rules after this one. If you remove this section you will most likely get a server 500 error, so leave it in.

Now, this ReWrite rule just tells to change the URL if someone types in www.[DOMAIN NAME].com/contact and pull up the normal contact page. It works just like the other ReWrite rules except it isn’t using the database so just add it to your .htaccess file so it looks something like this:


# 2. In your opencart directory rename htaccess.txt to .htaccess.

# For any support issues please visit: http://www.opencart.com

Options +FollowSymlinks

# Prevent Directoy listing 
Options -Indexes

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

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base  [L]
#NEW REWRITE RULES
RewriteRule    ^contact/?$    index.php?route=information/contact  [L]  #Contact Page
#END NEW REWRITE RULES
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]

### Additional Settings that may need to be enabled for some servers 
### Uncomment the commands by removing the # sign in front of it.
### If you get an "Internal Server Error 500" after enabling any of the following settings, restore the # as this means your host doesn't allow that.

# 1. If your cart only allows you to add one item at a time, it is possible register_globals is on. This may work to disable it:
# php_flag register_globals off

# 2. If your cart has magic quotes enabled, This may work to disable it:
# php_flag magic_quotes_gpc Off

# 3. Set max upload file size. Most hosts will limit this and not allow it to be overridden but you can try
# php_value upload_max_filesize 999M

# 4. set max post size. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value post_max_size 999M

# 5. set max time script can take. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_execution_time 200

# 6. set max time for input to be recieved. Uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_input_time 200

Upload it to your root OpenCart folder and then try it. If you type www.[DOMAIN NAME].com/contact it will show up the normal contact us page! Hurrah!

Unfortunately, we’re not quite done yet. We need to change the links in OpenCart to point to the new contact us page URL rather than the old one so I’ll go through a few of them but bear in mind that there might be some I’ve missed you’ll need to check.

Open up the following file in your text editor:

catalog/view/theme/[YOUR THEME]/template/common/footer.tpl

Find and replace the following:

<a href=”<?php echo $contact; ?>”><?php echo $text_contact; ?></a>

to:

<a href=”http://www.[DOMAIN NAME].com/contact”><?php echo $text_contact; ?></a>

This changes the contact us link at the bottom of the page. The next file will change the contact page link shown to users to have successfully checked out.

catalog/language/[YOUR LANGUAGE]/checkout/success.php

Have a look for the section in both of the top two lines where it tells the user to direct questions to the store owners, in that line you will see a link which needs changing as below:

<a href="%s">

changes to

<a href="http://[DOMAIN NAME].com/contact">

which changes the other link.

There will be other references to the contact us page but you can leave a comment below and I will tell you which files you need to be editing to make them point to your new URL!

Changing the account page URL in OpenCart

OK, another URL which everyone would like to see edited is the account page. So, I’m going to go through the steps again just to show you how it applies in this case. You could probably guess what the .htaccess rule is going to be but I’ll add it below anyway:

RewriteRule ^account/?$ index.php?route=account/account [L] #Account Page

In this rule we are using [DOMAIN NAME].com/account as the link to the account page and calling index.php?route=account/account when the .htaccess file sees it in the URL. Nice and easy.

This one is slightly different though, we need to go into the login function and change the URL to which the system redirects when a user successfully logs in.

Open up the following file:

catalog/controller/account/login.php

and find the following line and replace it for the link you want to use as shown below:

$this->redirect($this->url->link('account/account', '', 'SSL'));

changes to

$this->redirect('http://[YOUR DOMAIN].com/account')

And there we go, we’ve changed the account link for the OpenCart store to something more obvious for users. You can have more than one unique .htaccess rule if you want to use multiple links so your file will look like this:


# 2. In your opencart directory rename htaccess.txt to .htaccess.

# For any support issues please visit: http://www.opencart.com

Options +FollowSymlinks

# Prevent Directoy listing 
Options -Indexes

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

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base  [L]
#NEW REWRITE RULES
RewriteRule  ^contact/?$  index.php?route=information/contact  [L] #Contact Page
ReWriteRule  ^account/?$  index.php?route=account/account  [L]  #Account Page
#END NEW REWRITE RULES
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]

### Additional Settings that may need to be enabled for some servers 
### Uncomment the commands by removing the # sign in front of it.
### If you get an "Internal Server Error 500" after enabling any of the following settings, restore the # as this means your host doesn't allow that.

# 1. If your cart only allows you to add one item at a time, it is possible register_globals is on. This may work to disable it:
# php_flag register_globals off

# 2. If your cart has magic quotes enabled, This may work to disable it:
# php_flag magic_quotes_gpc Off

# 3. Set max upload file size. Most hosts will limit this and not allow it to be overridden but you can try
# php_value upload_max_filesize 999M

# 4. set max post size. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value post_max_size 999M

# 5. set max time script can take. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_execution_time 200

# 6. set max time for input to be recieved. Uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_input_time 200

So, there we go

By now you’ll be familiar with the principles of changing the base URLs for core OpenCart pages. The hard work is finding all of the links and pointing them in the right direction but as I mentioned earlier just leave me a comment if you want any help finding the right files to edit. Below are a full list of URL changes written as .htaccess:

ReWriteRule  ^home/$  index.php?route=common/home [L] #Home Page
RewriteRule  ^contact/?$  index.php?route=information/contact  [L] #Contact Page
ReWriteRule  ^account/?$  index.php?route=account/account  [L]  #Account Page
ReWriteRule  ^login/?$  index.php?route=account/login  [L]  #Login Page
ReWriteRule  ^logout/?$  index.php?route=account/logout  [L]  #Logout Link
ReWriteRule  ^cart/$  index.php?route=checkout/cart  [L] #Cart Page
ReWriteRule  ^checkout/$  index.php?route=checkout/checkout  [L] #Checkout Page
ReWriteRule  ^manufacturers/$  index.php?route=product/manufacturer  [L] #Manufacturer Page
ReWriteRule  ^specials/$  index.php?route=product/special  [L] #Specials Page
ReWriteRule  ^vouchers/$  index.php?route=account/voucher  [L] #Voucher Page
ReWriteRule  ^wishlist/$  index.php?route=account/wishlist  [L] #Wishlist Page
ReWriteRule  ^my-orders/$  index.php?route=account/orders  [L] #Past Orders Page
#END NEW REWRITE RULES

Well, this is a mammoth post and I hope you found it useful. Let me know how you get on and ask any questions you like by leaving me a comment below as usual!

Jack W. Davis

Jack W. Davis

I am a software engineer specialising in PHP/MySQL based web applications. Originally from a marketing background, I enjoy coding, reading and gaming. Currently exploring NodeJS.

Read More