Trying to get Product Name and product ID into url with slash at the end and in between without ?p=

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

Answers


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.


Need Your Help

How can I set the schema dynamically for a Code First DbContext?

ef-code-first entity-framework-6 database-schema

I have an EF6 Database First application that uses tables generated to an edmx from the dbo schema.