Forum Moderators: phranque
to
mydomain/product_info.php/product_id/33
i have tried this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^product_info\.php/cPath/([0-9]+_[0-9])/product_id/([0-9]+)$ [mydomain...]
but this does not seem to work can someone point me in the errors of my ways thanks in advance
RewriteRule ^product_info\.php/cPath/([0-9]+_[b][0-9]+)/[/b]product_id/([0-9]+)$ http://example.com/product_info.php/product_id/$2[R=301,L]
RewriteRule ^product_info\.php/cPath/[0-9]+_[0-9]+/product_id/([0-9]+)$ http://example.com/product_info.php/product_id/$1[R=301,L]
http://example.com/Epson-Printer-Cartridges/Epson-Photo-R2400-Series/1212323/
to
http://example.com/1212323/
i would like to do this do a few url's I could esily do it but the problem is this bit Epson-Printer-Cartridges/Epson-Photo-R2400-Series is not constant all i want is the end bit
any sudjestion on where I would start
Thanks in advance
[edited by: jdMorgan at 1:08 am (utc) on Jan. 13, 2006]
[edit reason] example.com [/edit]
1) Always contains "/<numbers>"?
2) Always contains "/" followed by seven digits?
3) Always contains "/" followed by seven digits followed by "/"?
4) Always contains "/" followed by more than x digits?
5) Always contains "/" followed by more than x digits followed by "/"?
6) Always contains "/" followed by more than x but less than y digits?
7) Always contains "/" followed by more than x but less than y digits followed by "/"?
How specific can you get in defining what the URL looks like? This is the art of writing good regular-expressions patterns, and also defines the art of designing (yes, that's the operative word) good URLs. For example, they're boring as all, but /prod and /cat -style URLs exist for a very good reason -- anything that follows those prefixes is sure to be a product or category number. That makes the regex simple and efficient.
Basically, mod_rewrite depends on you to define good patterns and use good URL design. If you fail either task, then mod_rewrite will happily rewrite both the URLs you want to rewrite, and many more that you don't. :o
Once you can define your URLs in the terms like I listed above, converting that list to regular expressions patterns for mod_rewrite is a lot easier.
Jim
RewriteRule ^Epson-Printer-Cartridges/[^/]+/([^/]+)/?$ http://www.example.com/$1 [R=301,L]
The modified pattern says, "Look for requests starting with 'Epson-Printer-Cartridges' followed by a slash, followed by one or more characters not equal to a slash, followed by another slash, followed by one or more characters not equal to a slash (store these in $1), and ending with an optional slash."
If you use multiple ".*" patterns, which mean "any number of any character", then the regular-expressions processing has to work very hard to figure out how much of the requested string to 'match' with each piece of your pattern. Because ".*" is a 'greedy' pattern, on the first pass, regex will try to match everything after 'Epson-Printer-Cartridges/' into the first ".*". It will then fail, because that leaves nothing to match the rest of the pattern. So, it will back off one character and try again, fail, and keep trying until it can figure out where the 'boundaries' are between the various pieces of your pattern. The number of trials will depend on the length of the requested URL.
This problem can be entirely avoided by using the negative-match patterns I show. That is because regex knows when to stop trying to match in each subpattern -- we've told it to stop when it finds the next slash.
Sometimes, using ".*" is unavoidable. But when it is avoidable, don't use it.
Jim