Question

I currently have links at my site like this:

http://www.domain.com/locations/locationProfile.php?locationID=22

I am hoping, for SEO purposes that I could change this to something like this:

http://www.domain.com/locations/southern-maryland

"Southern Maryland" is currently pulled from a mysql db as the location name for location ID 22. Is it even possible to get this into the URL when my site structure currently utilizes the less attractive first version?

Was it helpful?

Solution

You can't use htaccess to do this for you. It's possible to use a RewriteMap that you can define in your server config (can't define it in htaccess file), but it's far easier if you just do this in your locationProfile.php script.

You can detect if the request is made with the query string using the REQUEST_URI value:

if ( $_SERVER['REQUEST_URI'] == '/locations/locationProfile.php' &&
     isset($_GET['locationID'])) {
    // go into DB and extract the location name then redirect
    header('Location: /locations/' . $location-name);
}

Then in your htaccess file in your document root):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^locations/([^/]+)$ /locations/locationProfile.php?locationName=$1 [L,QSA]

And finally, make your php script look for the locationName parameter and server the proper page based on that instead of the ID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top