Search Engine Optimization (SEO) is all about getting increased traffic to your site by ensuring that key product information is easily accessible by search engines. A good SEO strategy improves product and category URLs by removing query parameters and making the URL more human readable. However, changing URLs can lead to broken links or file not found exceptions. To prevent these issues, a 301 redirect is created between your old and new URLs. A 301 redirect occurs when a shopper tries to access a product by using the old URL. The shopper is automatically forwarded to the new URL. When a search engine tries to re-index an old URL, the 301 status alerts the search engine to index the new URL instead.
You can create a 301 redirect between legacy URLs and SEO URLs in different ways. A common method is to set up web server mod rewrite rules in the web server configuration file. This method might require a large number of redirect rules - a rule for each category and product on the site. Usually, it is impossible to create a single redirect rule to handle every redirect to SEO URLs. Another method, demonstrated in this article, is to customize the web application layer to automatically redirect the URLs.
In this article, you learn how to customize WebSphere Commerce to automatically redirect between legacy URLs and SEO friendly URLs while preserving the search engine rankings.
The web application layer is customized to perform additional logic required to find mappings between legacy and SEO URLs.
After completing this article, the system will automatically forward URLs, such as:
/ProductDisplay?catalogId=10501&storeId=11201&productId=14203 &langId=-1&top_category=10529&parent_category_rn= 10530/Product2_11201_10501_14203_-1_10512_10530_10530_ProductDisplayErrorView
To a WebSphere Commerce V7 Feature Pack 3 SEO friendly URLs, such as:
Note the following limitations:
- Legacy URLs redirect to canonical SEO URLs in a format that does not contain the full hierarchical path.
- This article does not demonstrate how to simplify the context root. For instructions, see the Shortening the context root of a store URL topic in the WebSphere Commerce Information Center.
- This article demonstrates how to redirect product and category pages. It does not show how to redirect other content, such as static pages or pages in the checkout flow.
- WebSphere Commerce V7 Developer is installed.
- WebSphere Commerce V7 Feature Pack 3 or later is installed.
- A starter store, which uses the new SEO URLs (Madisons-FEP, or Elite-FEP) for WebSphere Commerce V7 Feature Pack 3, has been published.
Overview of steps
The high-level steps described in this article are:
- Create a custom
SEOURLMapperclass to handle additional redirects between legacy and new URLs.
- Create a query in a query template file that is used by your extended
- Modify the
wc-server.xmlfile to use the custom SEOURLMapper class.
Create a custom SEOURLMapper class
- Open your WebSphere Commerce Development environment.
- In the Enterprise Explorer view, expand the WebSphereCommerceServerExtensionLogic project.
- Right-click src, then click New > Package,
- In the Name field, type
mycompanyis your company prefix.
- Click Finish.
- Right-click the new package and then click New > Class.
- In the Name field, type
ExtendedSEOURLMapperImpl, and then click Finish.
- Download the code sample file that is provided with this article, code_sample.zip, for the extended SEO URL Mapper class.
You have extended the SEO URL Mapper class to redirect the legacy URLs to the canonical form of your new SEO URLs. Look at the javadoc and inline comments in the provided code sample file.
Create a query template file to hold SQL queries
- Go to the WC/xml/config directory.
- If it does not exist, create a directory named
- Right-click config.
- Click New > Folder.
- In the New Folder dialog, enter the name of
- Click Finish.
- Right-click the directory you created, then click New > File. The New File dialog opens.
- In the Name field, type
wc-query-utilties.tpland click Finish.
- Enter the code snippet shown in Listing 1 into the file, then click
File > Save.
Listing 1. Extended Query Template file
<!-- =======================================================--> <!-- This SQL will return the active SEO URL keyword record --> <!-- of a catalog entry or catalog group in a specified --> <!-- language and store. --> <!-- @param objectId The ID of the catalog entry or catalog --> <!-- group. --> <!-- @param langId The language ID of the keyword. --> <!-- @param storeId The store ID of the keyword. --> <!-- ====================================================== --> BEGIN_SQL_STATEMENT base_table=SEOURLKEYWORD name=MyCompany_Select_ActiveKeyword sql= SELECT * FROM SEOURLKEYWORD, SEOURL WHERE SEOURL.TOKENVALUE = ?objectId? AND LANGUAGE_ID = ?langId? AND ( STOREENT_ID= ?storeId? OR STOREENT_ID IN (SELECT RELATEDSTORE_ID FROM STOREREL WHERE STORE_ID = ?storeId? AND STRELTYP_ID = -4) ) AND SEOURLKEYWORD.SEOURL_ID = SEOURL.SEOURL_ID AND SEOURL.TOKENNAME = ?tokenName? AND STATUS = 1 END_SQL_STATEMENT
You have now created a query file to hold all the SQL statements that the extended SEOURLMapper requires to get information from the database.
Update the server configuration file to use the custom SEOURLMapper class
- In WebSphere Commerce Developer, go to the WC\xml\config directory.
- Open the wc-server.xml file for editing.
- Find the node named
SEOConfiguration. Add the following code as a child node under the SEOConfiguration node:
<mapper-class-name value="com.mycompany.commerce.seo.url.helpers. ExtendedSEOURLMapperImpl"/>
mycompanyis your company prefix. For example:
<SEOConfiguration defaultUrl="" dynamicUrl="true" enable="true"> <mapper-class-name value="com.mycompany.commerce.seo.url.helpers. ExtendedSEOURLMapperImpl"/> </SEOConfiguration>
- Save and close the file.
- Restart your WebSphere Commerce server.
You have modified the web application to perform additional logic to check for full URL redirects on the storefront.
Test your customization
In this section, you can test that your customization works as expected:
- Find a legacy URL to a product or category page. The legacy URL has a
format similar to the following example (also see the example shown in
Figure 1. Store page with the legacy URL
- In a web browser, enter the legacy URL.
- Observe that the URL is reformatted into the new SEO friendly URL
structure, as shown in Figure 2.
Figure 2. Store page with the redirected SEO URL
In this article, you learned how to create a custom SEOURLMapper class that handles legacy URLs and redirects them to SEO friendly URLs. This way, you can ensure users who have bookmarked your legacy URLs are still be able to link to your products using the new SEO URLs. Also, search engines who have indexed their search results against your legacy URLs will now forward to your new SEO URLs and receive a 301 redirect informing them to re-index.
- WebSphere Commerce V7 Information Center
- WebSphere Commerce Information Center: Search engine optimization
- WebSphere Commerce Information Center: URL redirects for SEO
- developerWorks Commerce zone
Dig deeper into Commerce on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Keep up with the best and latest technical info to help you tackle your development challenges.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.