Petr Novák
Petr Novák

Reputation: 115

URL routing from database

I have database table in this structure:

id  title description URL  
1   abc   abc desc    abc  
2   xyz   xyz desc    xyz-123  
3   tit   Description web-description  
...

I need to generate web pages from my database table such as:
domain.com/abc
domain.com/xyz-123
domain.com/web-description

I dont´t know where and how to set rewrite rule to take it from my database table. I don´t want to use robust framework such as Zend, I want only sample code to solve this problem. Please, any ideas? Thanks!

Upvotes: 1

Views: 706

Answers (2)

Sathiska
Sathiska

Reputation: 525

You can do this from php and .htaccess file.

  1. Make text file name with .htaccess and save on your document root.
  2. Put following content into .htaccess file.
  3. Your php file name should be index.php

    RewriteEngine On

    RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1

    RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?id=$1

Thats all.

When you execute url another url has executed.

Ex:

domain.com/xyz-123 will execute domain.com/index.php?id=xyz-131 domain.com/web-description will execute domain.com/index.php?id=web-description

Note: Apache rewite module should be enabled

Upvotes: 2

David Jashi
David Jashi

Reputation: 4511

You can read this tutorial, but you should really edit your question title to remove irrelevant "database" part from it.

Upvotes: 0

Related Questions