TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   mod_rewrite how to get title from sql table in url but use id? (http://www.talkphp.com/general/6344-mod_rewrite-how-get-title-sql-table-url-but-use-id.html)

ramtindow 08-18-2012 04:15 PM

mod_rewrite how to get title from sql table in url but use id?
 
hello, pleaaase help me!
I have a php file "page.php" witch it gives the variable "id" from the url and shows a record WHERE id=$_GET['id'] .

I want to replace urls like "example.com/books/page.php?id=$id" (where $id can be anything numeric) with sth like "example.com/books/$id/title/"

where title and id are columns of sql table.

I want to know what to write in .htaccess file and how it gets the title from sql and writes it in front of $id in the url.

also I would like to titles Spaces be replaced by Dash sign.


my table is like :

id | title |
1 | an example |

witch then we should be able to use url "example.com/books/1/an-example/" instead of "example.com/books/page.php?id=1" .

Thanks a lot!

tony 08-19-2012 02:34 PM

There are tutorials about URL rewrite everywhere in the internet. But for completion purposes A quick example of how to do rewrite:

Code:

# Turn on URL rewriting
RewriteEngine On

# Rewrite titles and IDs
RewriteRule ^books/([0-9]+)/(.*)/?$ books/page.php?id=$1 [L]

Don't forget to enable mod_rewrite in your apache configuration.
The gist of rewrites is if you know regular expressions, you know how to do it.

Here is a cheatsheet for Apache rewrite.

All this is assuming you use Apache for web hosting server.

P.S. I didn't test the rewrite rule, just came about using your criteria

About this part:

Quote:

Originally Posted by ramtindow (Post 33225)
witch then we should be able to use url "example.com/books/1/an-example/" instead of "example.com/books/page.php?id=1"

I don't know what you meant by this, you already have a valid id to query for a book in the database. I don't see why you need to ids (id and title).

I hope this helps.


All times are GMT. The time now is 06:50 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0