views:

45

answers:

1

Hello,

I guess the title almost says it all.

My original url looks like: http://www.something.com/buy/index.php?p=025823

The .htaccess I'm using looks like this:

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(\d+)*$ ./index.php?p=$1

The resulting url looks like: http://www.something.com/buy/025823

I'd like to end up with a url like: http://www.something.com/buy/Product Name/025823/

Can anyone help? Especially that Gumbo fella! LOL

A: 

If you can control the links that are generated to your /buy/ pages, just insert the product name in the URL (keeping in mind the rules of URLs, like no spaces allowed. Usually, people substitute a - for spaces). Then modify your rewrite regex to ignore the product name portion of your URL.

RewriteBase /
RewriteRule ^/buy/(.+)/(\d+)/$ ./index.php?p=$2

If you can't control the links, things get more complicated since you'll have to look up the product name using a RewriteMap and then perform a redirect. I just recently covered this is in a similar question. My answer to that question goes in to greater detail about using RewriteMap, but I don't think the details of that answer apply to your situation since using a statically-defined list of products probably won't meet your needs.

Basically, you'll need to implement a script that can be called from mod_rewrite's RewriteMap rule. You'll need something like:

RewriteMap prodname pgm:/path/to/script/namelookup.sh
RewriteRule ^/buy/(\d+)/$ /buy/${prodname:$1}/$1/ [R=permanent] 

Where namelookup.sh (or whatever) returns the product's name (on stdout) in a URL-encoded format. You'll definitely want to take a look at mod_rewrite's documentation about using RewriteMap, specifically the last section of the RewriteMap Directive on using an external rewriting program.

josh3736
Fantastic! Josh, you da man! I've been trying for two days to get this and neglecting other client's work. Here's what I ended up with. Slightly modified since my .htaccess is in the buy directory. Worked just fine right out of the box. Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)/(\d+)/$ ./index.php?p=$2 Thanks again!
Fisherman
Looks like the comments don't allow for proper paragraphs. Sorry.
Fisherman
@Douglas: I'm glad I could help. Since this answer worked for you, remember to click the green checkmark above to mark this answer as accepted and your question resolved.
josh3736