How to: Use ModRewrite in .htaccess to include search engine friendly keywords in URLs

October 19th, 2009

Most CMS’s offer permalinks in the form of keywords within the URL. Using ModRewrite you can easily accomplish this.

First verify ModRewrite is enabled in Apache.

Create a test folder within your web root path and then create a file named .htaccess in your new folder containing this text:

RewriteEngine On

Now visit yourdomain.com/testfolder. If you get a 500 / Internal Server Error then you need to enable ModRewrite in Apache.

Once the page is loading without a 500 error, we can continue.

I’m going to show you how to turn an ugly URL like:

http://www.yourdomain.com/index.php?pageid=49382

to

http://www.yourdomain.com/pages/jewelry-and-watches/49382

First, let’s open .htaccess back up and add the following line:

RewriteRule ^([^/]+)/([0-9]+)[/]?$ index.php?keywords=$1&id=$2

Just to explain, the line above is rewriting any requests like:

/testfolder/these-are-my-keywords/123

to

index.php?keywords=these-are-my-keywords&id=123

Remember it’s not REDIRECTING, it’s REWRITING. This happens blindly to the user/spiders, so it can add true value to your web structures.

Next create a file named index.php in the same test folder with the following contents:

<?php
  //dump out $_GET variables
  print_r($_GET);
?>

You can now visit the page in your browser to see it in action.

Example URL:

http://www.yourdomain.com/testfolder/the-keywords-you-want/12345

You should get output from index.php like:

Array ( [keywords] => the-keywords-you-want [id] => 12345 )

I have posted a PHP function that will keywordize a page title or description.

How To: Install ffmpeg on CentOS or Redhat using up2date or yum

September 30th, 2009

There are a bunch of instructions out there for installing ffmpeg. I found really easy instructions using yum, but I needed to use up2date. So I figured I would compile my steps.

First, make sure you’re logged in as root:

su -

Install with up2date:

1. Add the following line to /etc/sysconfig/rhn/sources:

yum dag http://apt.sw.be/redhat/el4/en/$ARCH/dag

Note: Your source line may be different depending on your release. You can find the correct line to use here: http://dag.wieers.com/rpm/FAQ.php#B5

2. Add the following line to /etc/ld.so.conf:

/usr/local/lib

3. Finally, execute each of the following in order:

ldconfig -v
rpm -Uvh ftp://rpmfind.net/linux/dag/redhat/el4/en/i386/dag/RPMS/rpmforge-release-0.3.6-1.el4.rf.i386.rpm
up2date ffmpeg ffmpeg-devel

Note: Your rpm may be different depending on your release. You can find the correct rpm to use here: http://www.rpmfind.net/linux/rpm2html/search.php?query=rpmforge-release

- Or – Install with yum:

1. Create and add the following to /etc/yum.repos.d/dag.repo:

[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1

2. Add the following line to /etc/ld.so.conf:

/usr/local/lib

3. Finally, execute each of the following in order:

ldconfig -v
rpm -Uvh ftp://rpmfind.net/linux/dag/redhat/el4/en/i386/dag/RPMS/rpmforge-release-0.3.6-1.el4.rf.i386.rpm
yum update
yum install ffmpeg ffmpeg-devel

Note: Your rpm may be different depending on your release You can find the correct rpm to use here: http://www.rpmfind.net/linux/rpm2html/search.php?query=rpmforge-release

You’re finished! You can find the PHP extension here: http://ffmpeg-php.sourceforge.net

I found the original yum instructions here: http://www.crucialp.com/resources/tutorials/server-administration/how-to-install-ffmpeg-centos-rhel-redhat-enterprise-easy-way.php

Title “keywordize” function

September 11th, 2009

This function is used to fetch a valid keyword string for use in a URL. This function comes in handy when you want to structure your URLs to contain keywords found in the page’s title, for example.

Any non alphanumeric characters are stripped from the string, and spaces are replaced with dashes. Extra whitespace is accounted for.

Example:

$title = 'This is the "Best" Function Ever!';
echo keywordize($title);

Output:

this-is-the-best-function-ever

Code:

  function keywordize($string){
    return preg_replace(array('/^[^a-z0-9]|[^a-z0-9]+$/', '/ +|(\.{2,}[ ]?)+/s', '/[^a-z0-9_-]/s', '/(-{2,})/', '/((^-)|(-$))/'), array('', '-', '', '-', ''), strtolower($string));
  }

You can use the output of this function in your URLs like most CMS applications do to make your URLs search engine friendly.

You can add weight to the keywords within you page by containing them within the page’s URL.

I’ve written a short tutorial on adding search engine friendly keywords to a URL with ModRewrite.