Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule not working

         

fzx5v0

10:57 pm on Jan 11, 2006 (gmt 0)

10+ Year Member



I am trying to rewrite
mydomain/product_info.php/cPath/21_34/product_id/33

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

jdMorgan

11:43 pm on Jan 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Missing plus sign:

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]

Also, you can simplify it, since you didn't use the $1 back-reference... No need to waste CPU time:

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]

Jim

fzx5v0

9:10 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



thanks that worke a treat another qustion is if i had a url's like this

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]

jdMorgan

1:21 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The trick is that you *must* nail down something. If you cannot guarantee the "beginning" or the "middle" of the URL, then what *can* you guarantee about the end of it?

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

fzx5v0

9:40 am on Jan 13, 2006 (gmt 0)

10+ Year Member



ok have have decided that Epson-Printer-Cartridges will be the constant

but everytime I try a rule I get 500 errors

RewriteRule ^/Epson-Printer-Cartridges/.*/(.*)/$ http://www.example.com/$1 [R=301,L]

I have tried different permutations but 500 all the time

fzx5v0

10:51 am on Jan 13, 2006 (gmt 0)

10+ Year Member



sorry I understand what the problem is now it was the accidental / at the start

Thanks for the help Jim I am slowly getting better at them

jdMorgan

4:56 pm on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try to avoid patterns with ".*" in them , and especially avoid those with more than one ".*" in them. They are very hard to process and therefore inefficient.

RewriteRule ^Epson-Printer-Cartridges/[^/]+/([^/]+)/?$ http://www.example.com/$1 [R=301,L]

will run much faster and will work even if the final "/" is omitted.

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