-1

I'm really sorry if i'm annoying you guys but this is my final question in regards to .htaccess tricks

I need wordpress style, 'pretty permalinks'

But It's too confusing for me.

I need, this url http://test.com/?page=test&ID=1 to be http://test.com/test/NAMEFROMDATABASE

How? I know how to get ID=1 by using $_GET['ID'], but how do I put a value from the database in the url, and read it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Why not do it `http://test.com/test/1/NAMEFROMDATABASE`, so you have the ID in your url, just like SO has them. This will help you do two things: 1. Will make the URLs shorter if need be. 2. Will make the lookup of the particular record easy and _fast_. – Shef Oct 03 '11 at 09:14
  • I'm currently doing it: test.com/djs?djID=1 the orignial url is test.com?page=djs&djID=1 But it's not SEO freindly, and it causes multiple results of the same djs page, plus i want users to type the name of a dj, not a random number then the dj name. – Billy Jake O'Connor Oct 03 '11 at 09:15
  • You can easily change that URL to `test.com/djs/1`. – Shef Oct 03 '11 at 09:18
  • I know but it's no use to me, I want test.com/djs/djname, Because it's seo freindly and easy for the users and everyone else to remember. – Billy Jake O'Connor Oct 03 '11 at 09:19
  • But lookup the short page name from a properly made DB table... Use some wits man... Then you ask. – gd1 Oct 03 '11 at 09:20
  • @ucario You can do it `test.com/djs/djname`, but that is going to require you store a unique version of the slug in the table, and it is going to make lookups quite slower compared to the integer lookups if you would make the urls `test.com/djs/1/djname`. – Shef Oct 03 '11 at 09:23
  • @Shef: if the pages are less than 100.000, then the performance gap would be hardly detectable even with proper instruments – gd1 Oct 03 '11 at 09:25
  • @gd1 I agree, but nothing beats the _"kill two birds with one stone"_ solution. :) – Shef Oct 03 '11 at 09:32

3 Answers3

6

you can not get ID value by $_GET['ID'] directly from this URL : http://test.com/test/NAMEFROMDATABASE.

You can get ID by following below logic.

  1. create link by category name. i.e. if you have category laptop then create link like http://test.com/category/CATNAME

  2. Write rewrite code in htaccess.RewriteRule ^category/(.*)$ categories\.php?CNAME=$2&%{QUERY_STRING} [L]

  3. in PHP code get category ID from category name.$catName=$_GET['CNAME']

OR

  1. create link by category name and category ID. i.e. if you have category laptop then create link like http://test.com/category/CATNAME-ID-CATID
  2. Write rewrite code in htaccess. RewriteRule ^category/(.*)-ID-([0-9]+)$ categories\.php?ID=$2&%{QUERY_STRING} [L]
  3. in PHP code get category ID directly. $catID= $_GET['ID']
Nimit Dudani
  • 4,840
  • 3
  • 31
  • 46
  • For me, RewriteRule ^djs/(.*)$ index\.php?page=djs&djID=$1&%{QUERY_STRING} [L] converts test.com/index.php?page=djs&djID=username into test.com/djs/username, $_GET['djID'] returns 'username', with no problems. – Billy Jake O'Connor Oct 03 '11 at 10:10
  • So thank you very much, it was only the $2 that didn't work for me, changing this to $1 worked perfectly. – Billy Jake O'Connor Oct 03 '11 at 10:11
1

How? I know how to get ID=1 by using $_GET['ID'], but how do I put a value from the database in the url, and read it?

You get the value from the database like so:

$id = mysql_real_escape_string($_GET['id']);
$sql = "SELECT folder, urlname FROM urls WHERE id = '$id' ";
// don't forget to single quote '$id'       ^   ^  or you'll get errors
// and even worse mysql_real_escape_string() will not protect you.
if ($result = mysql_query($sql)) {
  $row = mysql_fetch_row($result);
  $pagename = $row['urlname'];
  $folder = $row['folder'];
}

If you know id is an integer you can also use $id = intval($_GET['id']);
I recommend always using mysql_real_escape_string() because it works for all values and intval only works for integers.
In SQL it is never a problem to quote numbers, so make a habit of always quoting everything.
That way you cannot make mistakes.

You can never do

$sql = "SELECT urlname FROM urls WHERE id = '{$_GET['id']}' ";

Because that's an SQL-injection security hole.

See:
How does the SQL injection from the "Bobby Tables" XKCD comic work?
http://php.net/manual/en/function.mysql-query.php
http://php.net/manual/en/function.mysql-fetch-row.php
http://php.net/manual/en/function.mysql-connect.php
http://php.net/manual/en/function.mysql-close.php

Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319
  • Thank, you but perhaps I wasn't clear enough. I know how to get a value from a url and query it in the database. What I want to do, is convert a post ID into a POST name, eg ?id=1 becomes 'randomname' I only want this to be client side, so that test/randomname will be test/ID=1 – Billy Jake O'Connor Oct 03 '11 at 09:22
1

You can't do that in htaccess, you will need to adjust your script so instead of receiving id=1 will receive name=xxx. Than it will look for the name in database and compute the ID

Okay, so in .htaccess you'll have something like this

RewriteRule ^something/(.+)\.htm$ something/file.php?djname=$1 

In your php script you'll have

$name = mysql_real_escape_string($_GET['djname']);

$sql = "SELECT * FROM djtable where name='" . $name . "' LIMIT 1";

OBS: 1. Use proper escaping of the sql. 2. Make sure the dj names are distinct in the database.

Johan
  • 74,508
  • 24
  • 191
  • 319
Dan Bizdadea
  • 1,292
  • 8
  • 15
  • Thank you, that's all I needed to know. So can you now tell me, how do I change page=test&name=myname into test/myname. which I will then query in the database? – Billy Jake O'Connor Oct 03 '11 at 09:24
  • It's not the SQL! It's .htaccess, I'll code something now but how do I rewrite the URL, converting test.com?page=x&ID=y into test.com/x/y – Billy Jake O'Connor Oct 03 '11 at 09:29
  • @Dan, A: regarding escaping please follow your own advice. B: If `name` is unique you don't need `limit 1` C: `select *` is an anti-pattern, only select the fields that you need. – Johan Oct 03 '11 at 10:30
  • @Johan you are right, but here was not the case about writing good code/sql but about the htaccess. I just put the code to be more descriptive and avoid typing too much, because his own code/sql would be different. I agree that instead of adding observations i could have typed it better :) – Dan Bizdadea Oct 03 '11 at 10:34