View Full Version : URL rewriting and dynamic digg.
SEOMalc
10-23-2007, 10:10 AM
I have implemented URL rewriting for a news site and am now trying to implement DIGG buttons for each item of news.
Each news item has a clean URL i.e. /news/1345433/goldfish-eats-cat which is in fact pulling in news.php?newsItem = 1345433 via .htaccess.
When creating the DIGG button dynamically for each news item I'm getting the host and URI to construct the URL but they come in as the site.co.uk/news.php?newsItem = 1345433 rather than site.co.uk/news/1345433/goldfish-eats-cat
So...
Does anyone know of a way to reference the pre-rewritten URL ?
SEOMalc.
Jazajay
10-24-2007, 05:51 AM
Right this is the way I would do it, obviously I don't know how you have yours set up so you may need to slightly rework it.
Basically I would set the number in the DB and I would set the name of the article in the DB as well in a row connected to the number variable.
so to reach the name variable the query would look something like
$query = "SELECT name FROM news WHERE number ='$number'";
So you want to set up a query to get the DB number from the DB and another one to get the name as well (as above). To make it easier to explain lets say you have then saved them in $num and $name after you have retrieved them from the db.
Ok now you need to replace the space with a hyphen in the name variable and make it all lower case for it to work other wise all you will get is something similar to -
site.co.uk/news/1345433/Goldfish Eats Cat
and the .htaccess rule would make no sense and fail. To do this use the code below -
$pattern = "[[:space:]]"; //sets pattern to a space
$replace = "-"; //sets replace to the hyphen
$rewrite = ereg_replace($pattern,$replace,$name); //this replaces all spaces in the name with hyphens.
$rewrite = strtolower($rewrite); //this converts all uppercase letters to lowercase so the .htaccess rule is valid.
Ok now you have changed the name variable so it is SEO friendly and also what the .htaccess file is expecting you need to put it all together.
So to get -
site.co.uk/news/1345433/goldfish-eats-cat
you need to enter
<a href="http//;www.site.co.uk/news/<?php echo"$num/$rewrite"?>" />
This should then link to
site.co.uk/news/1345433/goldfish-eats-cat
Obviously I haven't tested it so there could be a few syntax errors it looks ok but you will just have to try it and see.
The logic is there and the code should be fine.
Jaza
SEOMalc
10-24-2007, 06:19 AM
Cheers for that Jaza, that way would definitely work.
I came up with a simpler solutions for my particular case while trying to get my 23 week old son back to sleep at 3.00am this morning.
I simply modded my .htaccess rules to...
^/(blahblah)/([0-9]*)/(.*)$ news.php?start=$1&newsItem=$2&finish=$3
and built the URLs when I needed them using...
<?php echo "http://www.blah.co.uk/" . $_GET['start'] . "/" . $_GET['newsItem'] . "/" . $_GET['finish'] ?>
Just implemented it now and it seems to work perfectly. I just feel a little bit dirty especially having now considered your solution.
SEOMalc.
Jazajay
10-24-2007, 12:06 PM
No worries I've been there with the 3am feeds one of the best times for problem solving especially if you think about it before you go to bed as your brain natural thinks about it while you sleep.
As always there is a thousand ways to accomplish anything with PHP so I'm glad to help
:)
Jaza