Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite how to get title from sql table in url but use id?

         

ramtindow

5:35 pm on Aug 18, 2012 (gmt 0)

10+ Year Member



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!

g1smd

9:23 pm on Aug 18, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



htaccess gets the title from the requested URL once the link on the page has been clicked.

The first step is to link to the right URLs from the pages of your site.

htaccess acts only after the link is clicked.

lucy24

9:47 pm on Aug 18, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What have you got so far? The necessary pieces are

#1 A few lines in htaccess to REWRITE (not redirect) request for pretty URL to php file

#2 php file to look up correspondence between requested URL and real page content, ending up with further REWRITE to show user the page they wanted.

and

#3 A few more lines in htaccess to REWRITE request for old-fashioned ugly URL to a PHP file

#4 php file which does the same type of lookup as #2, only in the opposite direction, and then REDIRECTs to the pretty URL. This php file probably uses the same database as #2 and may even be the same physical file, depending on how you prefer to write your php.

Pieces #1 and #2 are functionally essential. Pieces #3 and #4 are functionally optional but SEO-essential. That is, your site will work without them, but SERPs will be a mess.

Now go back a few threads in the Apache forum. Look for some paste-in boilerplate headed "Why We Make You Do It Yourself". We'll help, sure, but we won't write your code for you.

g1smd

10:00 pm on Aug 18, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



further REWRITE

Just one rewrite, not "further".
--------------------------------------



These are the steps...

Link to
href="/123-page-title"
from the pages of your site. URLs are defined in links.

User clicks the link and requests
example.com/123-page-title


In htaccess, rewrite the request to the internal server file at
/index.php?id=123&name=page-title


The index.php script performs several functions:
- looks in database to see if record 123 exists - send 404 if not,
- looks in database to see if page-title is the correct page title for record 123 - use PHP HEADER directive to send 301 redriect to the correct URL if not, or
- retrieves the page title, meta description, page content, builds the page and sends it.


To stop duplicate content indexing you also need to use htaccess to intercept external requests for
example.com/index.php?id=123&name=page-title
and rewrite (that's rewrite, not redirect) these to
/special-script.php
.

This special script looks in the database and sends 404 if the requested ID does not exist. If the URL does exist, uses the PHP HEADER directive to 301 redirect to the correct "friendly" URL.


The whole thing is less than a dozen lines of mod_rewrite code and a couple of dozen lines of PHP code.

ramtindow

8:47 am on Aug 20, 2012 (gmt 0)

10+ Year Member



Thank you! I've got how to do it.
in the directory "books" in the .htaccess file we can write:

RewriteEngine On
RewriteRule ^([0-9]+).*\/ page.php?id=$1

and for replacing Spaces of the titles with Dashes in links, we can use this "str_replace" function :

$goodtitle=str_replace(" ","-",$title);

(wrote these for others may have such problem.)

g1smd

6:26 pm on Aug 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



^([0-9]+).*\/

Absolutely not. You MUST capture and validate the "text" part of the URL, otherwise you leave your site wide open to malicious linking of the
/3561-this-product-is-junk
type, as well as creating infinite duplicate content.

RewriteRule ^([0-9]+)-(.*) /script.php?id=$1&name=$2 [L]


URLs for pages should not end in a trailing slash. Trailing slash denotes folder or index page in folder. Slashes in htaccess should not be escaped.

g1smd

10:27 pm on Aug 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's a lot more than "replace space with hyphen" you should do to the title to make it usable for the URL:

$thisProductName = preg_replace('/[^a-z0-9]+$/', '', (preg_replace('/[^a-z0-9-]+/', '-', (preg_replace('/[\']+/', '', (preg_replace('/\ ?&\ ?/', '-', strtolower(trim($this->productName)))))))));