Magento – URL Rewriting Tutorial

How to manage URL rewriting in Magento

Search engine friendly URLs improve the indexing and ranking of your site, make it easier for people to come across your site when searching through search engines for particular keywords, and generally are easier to remember and improve the navigation on your site.

When you add a product or category Magento automatically generates a SEF URL based on the name of the product or category. When you add or edit a product/category you can specify the URL yourself instead of leaving that task to Magento. In addition to this, Magento has a built-in URL rewrite manager from which you can manage all the existing product and category SEF URLs, and you can also add custom URL rewrites.

In this tutorial we'll examine the various URL rewrite settings.

Product, Category and CMS page URL Settings

By adding or editing a product or a category (and also a static page) you can configure a setting labeled URL Key. It allows you to set the URL address of the particular product/category. For example, when you add a product by going to Catalog menu>Manage Products>Add Product button, or when you edit a product by going to Catalog>Manage Products>Edit button for the product that you want to edit, on the general page that's displayed by clicking on the General button on the left you'll see the URL Keyoption:

Setting the URL Key of a Product

If you're adding a product and you leave the field for URL Key empty, Magento will generate the URL key automatically by using the name of the product. For example, if we add a product named HostKnox Sneakers, Magento will generate the URL key hostknox-sneakers; it will simply turn all uppercase letters into lowercase ones, and it will put hyphens between the words. If you type a URL key yourself, use lowercase letters and separate words with hyphens. Don't put a slash at the beginning of the URL key. The URL key itself is a relative path that's added to the base URL address for the store. For example, if the store is accessible at domain.comand the URL key of the product is hostknox-sneakers, the URL address of the details page of that product will be domain.com/hostknox-sneakers.html. If you're editing the URL key of an existing product, it's recommended to leave marked the checkbox for Create Permanent Redirect for old URL (it's just under the URL Key field).

An identical URL Key option is available when you add/edit categories. It's displayed on the General Information page:

Setting the URL Key of a Category

The same things apply to this setting as compared to the one for products.

URL keys can also be set for static/CMS pages. When you go to CMS menu>Pages>Add New Page button, or to CMS menu>Pagesand you click on an existing page listed in the table to edit it, you'll see the option (it's displayed by the Page Information button on the right):

Setting the URL Key of a CMS Page

Note that unlike products and categories, when you add pages you have to type something in the URL Key field in order to be able to save the page. It's required and if you leave it blank Magento will not generate an URL key automatically.

Search Engines Optimization System Setting

There's a system setting from which you can turn on and off web server rewrites. To access this option, in the admin panel of your Magento go to System menu>Configuration, on the page that opens click on the Web button under the General menu on the left, and from the panels that appear on the right select Search Engines Optimization to expand it. The panel contains the option Use Web Server Rewrites and it's set to No by default:

Web Server Rewrites Option

If you want to, set it to Yes and click on the Save Config button in the upper right corner. Note that even if you don't enable this option, you'll still be able to set an URL key when you add/edit products and categories.

URL Rewrite Management

The Magento admin panel has a built-in URL rewrite manager from which you can edit the existing URL rewrites and you can also add new custom rewrites. To access it go to Catalog menu>URL Rewrite Management. On the page that opens you'll see a table with the existing rewrites:

URL Rewrite Management Page

To edit a rewrite click on its Edit button or just on its row in the table. This will display several settings:

Rewrite Manager-Editing a Rewrite

Most of these settings actually cannot be edited once the particular rewrite is created. The Type is set to System which means that the URL rewrite has been generated by Magento, and it's not a custom rewrite. It cannot be changed. The Target Path option cannot be changed either. This is the actual URL path to the particular page. The Request Path is the URL rewrite itself. This is the Search Engine Friendly URL that customers can use to access or link to the page (rather it's only the relative path that's added to the base URL). Keep in mind that people can actually use both URL addresses to access the particular page. So, if we use our example screenshot, domain.com/hostknox-sneakers.html and domain.com/catalog/product/view/id/17 will both open the same page.

The Redirect option determines what will be shown in the address bar of the user's web browser. It's set to No by default which means that when the customer uses a link to the particular page or types the SEF URL in the address bar (e.g. domain.com/hostknox-sneakers.html), the SEF URL will be displayed in the address bar of the browser after the request is resolved. If you set the Redirect drop-down menu to Temporary or Permanent, even when the customer types the SEF URL, the target URL address (e.g. domain.com/catalog/product/view/id/17) will be shown in the address bar after the request is resolved.

The other setting that you can configure is Description. This is not something that will affect the URL address or anything on the frontend of your site. In the text area that's provided for this option you can simply put some notes regarding the rewrite.

If you make any changes, don't forget to click on the Save button.

Of course, you can also change the URL rewrite of each product and category from the settings of that product or category. We already described how to do that in the first section of this tutorial.

In addition to editing existing rewrites you can add new ones. This can only be done from the rewrite manager. You can do this if, for example, you want to have a few URL addresses that point to the same category or product page. To do this on the page with the table with the existing rewrites click on the Add URL Rewrite button. It's above the top right corner of the table:

Rewrite Manager-Adding a URL Rewrite

At the top of the page that opens there's a drop-down menu Create URL Rewrite from which you can select For categoryFor product and Custom. If you select For category, this will show a list of the categories that you have created. When you click on one of the categories the URL rewrite settings will appear; these are the same settings that are displayed when editing a rewrite. You can change the Request Path setting, the Redirect setting, and you can also add a description.

If you select For product from the Create URL Rewrite drop-down menu, a list of the products in your catalog will appear:

Rewrite Manager-Create URL Rewrite Menu

Click on the product for which you want to add a rewrite. After you do that a list with the existing categories will appear from which you can select the category to which the product belongs, or you can use the relative button to skip category selection:

Rewrite Manager-Selecting or Skipping Categories When Adding a Product Rewrite

When you add a product a URL rewrite is created with respect to the base URL (e.g. domain.com/hostknox-sneakers.html) and with respect to the category to which the product belongs (e.g. domain.com/shoes/hostknox-sneakers.html). Both open the details page of the product. The target path of each of these rewrites is different. In our example the target path for hostknox-sneakers.html is catalog/product/view/id/17, and for shoes/hostknox-sneakers.html it's catalog/product/view/id/17/category/9. So the purpose of this explanation is to clarify that when you create a new rewrite for a product, if you select the category to which it belongs you'll add an alternative request path for the target path that has the form catalog/product/view/id/17/category/9 (the numbers are just examples); while if you click on the Skip Category Selection button, you'll create a rewrite/request path for the target path that has the form catalog/product/view/id/17.

Whether you click on the skip button or you select the category is not that important; the new rewrite in both cases will open the details page of the product. In any case, after you click on the button or on the category the rewrite settings will be displayed:

Rewrite Manager-Adding a New Product Rewrite

The settings will be preconfigured with the values of the original rewrite. To add the new rewrite delete the old one in the field for Request Path, type the new path and click on the Save button. Note that the old rewrite will not be deleted, you'll just add a new alternative path. For example, we'll change the request path shoes/hostknox-sneakers.html to shoes/sneakers.html, so that when a customer uses the URL addresses domain.com/shoes/hostknox-sneakers.html and domain.com/shoes/sneakers.html the same page will be displayed (the original target address for both is domain.com/catalog/product/view/id/17/category/9).

Another thing worth mentioning is that Magento generates rewrites/SEF URLs for products and categories for each store view; the generated paths will be the same. For example, if you have a multilingual store, the store view for each language will have a rewrite:

Rewrite Manager-Rewrites for Different Store Views

So when you want to change a SEF URL and you want it to apply to all store views you have to edit the rewrite for each view.

Revisions

No comments yet.

Leave a Reply