SEO Friendly URL’s with PHP
I had to come up with a quick little snippet of code to create some SEO friendly urls for dynamic content. What constitutes a SEO friendly url? Basically, just a url with a bunch of words and dashes, nothing to scientific about that. But it does make sense, as the url of this page will have a better chance of ranking well than a corresponding url of www.robertlbolton.com/articleId=21.
I could have hunted through Word Press’s code to find what they use, but I figured it would be quicker to just write it myself. I was trying to get away with using just one regular expression, but couldn’t make it happen. Without further ado,
$temp = preg_replace(’/(\W){1,}/’,'-’,$temp);
$temp = preg_replace(’/-$/’,'’,$temp);
echo $temp;
Here is the regular expression breakdown:
(\W) //match any non-word character
{1,} //match it one or more times (greedy matching)
/’
The second regular expression takes care of the trailing dash at the end that results when the string has spaces or non-word characters like exclamation points at the end. I think you could use a lookahead in the first reg exp to detect the end of the word and then not add a dash, but that is above my head.
July 22nd, 2006 at 4:02 pm
Wordpress was not displaying my backslash and the code was not working if you cut and paste directly from the html. I just put in the HTML character entity and now the code should work fine.
August 30th, 2006 at 5:07 pm
Hey, good post. I was stumped on how to remove that last ‘-’.
I find this code a little cleaner, and it also generated slightly cleaner output, IMHO. Punctuation characters are ugly.
function news_link_title ($t) {
return strtolower (
preg_replace( ‘/-$/’, ‘’,
preg_replace (
‘/[^a-zA-Z0-9]+/’,
‘-’,
$t
)
)
);
}
November 29th, 2006 at 8:33 am
Is there any special techniques that can be used to optimize a wordpress blog on my server for SEO. One issue I see is no way to change the title tags on each page, where it seems to take the blog name for the home page.
I have several hundred 600+ inbound links.
I have pinged Technorati manually and used pingoat as well as pingomatic every time I add a new blog.
There is plenty of content, about 30 articles.
What else can I do? What else should I do to optimize my blog?
November 29th, 2006 at 5:55 pm
Could make a blog post out of this…
1.Under permalink in the admin panel, add this to structure field: /%postname%
2.In the file /wp-content/themes/default/header.php, you can alter the title tags and meta description tag to display the name of the post
3. Make your title tags SEO friendly, by making it descriptive and including commonly searched words