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:
-
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; -
In the entry point script (
index.php
) you can get current URI from the$_SERVER['REQUEST_URI']
variable; -
Extract requested slug from the current URI using string functions or regexp (implementation will depend on your URL format);
-
Select page from database using simple condition:
WHERE slug = '{$currentSlug}'
and display it … -
… 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.