Slugs and htaccess

I need to rewrite my url’s to the slug I have stored for that page in the database. For an example my page will have an idea like pid = 1. I need to translate 1 to a slug that would be something like keyword-phrase. How do I do this?

Hi, gtibbetts1174

Actually, you don’t necessarily need .htaccess to achieve this, it can be done with any webserver, not only Apache with mod_rewrite.

Usually, you need to perform these steps:

  1. Make sure that all of your incoming requests use the same entry point - that means, no matter which URL is typed in the browser it should always lead to the same index.php file;

  2. In the entry point script (index.php) you can get current URI from the $_SERVER['REQUEST_URI'] variable;

  3. Extract requested slug from the current URI using string functions or regexp (implementation will depend on your URL format);

  4. Select page from database using simple condition: WHERE slug = '{$currentSlug}' and display it …

  5. … or show Error 404 if no pages were found by the current slug.

Take a look at the Front Controller pattern tutorial to learn more.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.