⊗ppPmSDHRU 443 of 447 menu

SEO-Friendly URLs in a PHP Engine

In the modern world, addresses with numbers like /page/1 are considered unattractive. It is more beautiful when a page is identified not by its number, but by a textual name corresponding to the page title, for example, like this: /page/my-first-page.

Such addresses are called SEO-friendly URLs (human-readable URLs). Having SEO-friendly URLs is more convenient for the user than meaningless id numbers. Moreover, search engines will improve our site's rankings for having them.

The part of the URL corresponding to the page name is called a slug. Let's add a column for slugs to our pages in the database:

pages
id slug title content
1 my-first-page title1 <div> content1 </div>
2 my-second-page title2 <div> content2 </div>
3 my-third-page title3 <div> content3 </div>

Let's now rework our engine to work with slugs instead of IDs. To do this, we'll fix the regex:

<?php preg_match('#/page/([a-z0-9_-]+)#', $url, $match); $slug = $match[1]; ?>

And in the page search condition, we'll specify the slug instead of the ID:

<?php $query = "SELECT * FROM pages WHERE slug='$slug'"; ?>

Rework your site engine to work with SEO-friendly URLs. Test its operation.

byenru