lucy24 - 8:07 pm on May 8, 2013 (gmt 0)
Overlapping previous posts, so this is purely a reply to your first:
Now the data I want to collect in variables is the portion after "www.example.com/" so in this case
I want "monster-bikes" and "p1c9"
Then using the collected variables the server can redirect to:
RewriteRule ^[\.0-9-a-z]+/([-a-z]+)/p([0-9]+)pg([0-9]+)\.html$ index.php?p=$2&c=$3&k=$1 [NC,QSA,L]
Several mistakes here.
Preliminary mistake: Your post says "redirect" but the rule creates a rewrite. That means the user's address bar won't change, but you secretly fetch content from where it lives. As it happens, this is good. You don't want a redirect; you want a rewrite (short pretty URL, long ugly "real" location). Just make sure you've got the terminology straight.
In the RewriteRule itself, the first mistake is the one that prevents the rule from ever matching: the initial
From your example it looks as if this represents the hostname. This isn't used in the body of a RewriteRule-- in fact it's hardly ever used at all in per-directory contexts such as htaccess. Leave it out.
Incidentally, literal periods don't need to be escaped inside grouping brackets. (This is a general Regular Expressions rule, not specific to mod_rewrite.) It won't do any harm; it just isn't necessary.
This part alone
should work. Use an opening anchor with no leading slash. Most Apache installations are happy to let you use \d instead of [0-9] at a savings of three bytes. But if you're not very experienced with Regular Expressions, the byte savings may be outweighed by your personal reaction of "What the ### does \d mean?"
The text of your post says the URL is "p1c2", but the quoted rules have "pg" in place of "c". I assume this is a simple typo.
The target of a rewrite should have a leading / slash. NOT the full domain name! Just the slash.
What's the "QSA" for? Is there any possibility that the original request already contained a query string? If not, leave off the flag. The server has to go looking for a query, and it will never find one. And in the present rule, it seems as if the whole point is that you start out with a nice query-less URL; the query gets added behind the scenes.
Also leave off the [NC] flag. This flag should never be used with rules that create a rewrite instead of a redirect. It doesn't prevent the rule from working; instead it allows the rule to work with an infinitude of different URLs. (Look up Duplicate Content.) It also means your php script has to do extra work, as it now has to regularize all those randomly cased parameters.