Forum Moderators: phranque
I get a URL like this
roast/hedgehog/224-4010.html
and I rewrite it to this
roast/hedgehog/224.html
using this rule
RewriteRule ^roast/hedgehog/([^/\.]+)-([^/\.]+)\.html$ /roast/hedgehog/$1.html
But now the boss wants to see it work like this
roast/hedgehog/224-4010.html
and I rewrite it to this
roast/hedgehog/4010.html
now if i change the above rule to
RewriteRule ^roast/hedgehog/([^/\.]+)-([^/\.]+)\.html$ /roast/hedgehog/$2.html
it work ok until i get a URL like this
roast/hedgehog/224-4010-123.html
It then produces
roast/hedgehog/123.html
but the boss wants it to produce
roast/hedgehog/4010-123.html
and that is where i am stumped, I know why it does it but i can't seem to figure a way around it and there are URLs that have many more - in them, I basically just need to loose the first one
Cheers
I played with it for awhile, and the simplest solution may be to use two rules:
RewriteRule ^boiled/badger/[^-/]+-([^./]+)\.html$ /boiled/badger/$1.html
RewriteRule ^boiled/badger/([^-/]+-)+([^-/]+-[^./]+)\.html$ /boiled/badger/$2.html
Jim
Thanks for that I have it working now after stretching out the code you gave me
I guess I didn't express it concisely enough in my explanation (that's definitely 90% of my problem), writing it as a rule makes it a bit more logical
"For number sequences of two parts, keep only the final part, and for number sequences of more that two parts, keep only the final two parts"
The rule should be
"for number sequences of 2 or more parts discard only the first part"
eg
roast/hedgehog/224-4010-123.html
should go to
roast/hedgehog/4010-123.html
and
roast/hedgehog/224-4010-123-678-975.html
should go to
roast/hedgehog/4010-123-678-975.html
basically the first grouping of digits (in this case the 224) needs to be discarded,
There can be a varying number of groups of digits within a file name
Using the below rules it now works (I have just expanded on what you gave me)
RewriteRule ^boiled/badger/[^-/]+-([^./]+)\.html$ /boiled/badger/$1.html
RewriteRule ^boiled/badger/([^-/]+-)+([^-/]+-[^./]+)\.html$ /boiled/badger/$2.html
RewriteRule ^boiled/badger/([^-/]+-)+([^-/]+-[^./]+-[^./]+)\.html$ /boiled/badger/$2.html
RewriteRule ^boiled/badger/([^-/]+-)+([^-/]+-[^./]+-[^./]+-[^./]+)\.html$ /boiled/badger/$2.html
RewriteRule ^boiled/badger/([^-/]+-)+([^-/]+-[^./]+-[^./]+-[^./]+-[^./]+)\.html$ /boiled/badger/$2.html
RewriteRule ^boiled/badger/([^-/]+-)+([^-/]+-[^./]+-[^./]+-[^./]+-[^./]+-[^./]+)\.html$ /boiled/badger/$2.html
RewriteRule ^boiled/badger/([^-/]+-)+([^-/]+-[^./]+-[^./]+-[^./]+-[^./]+-[^./]+-[^./]+)\.html$ /boiled/badger/$2.html
Nigel
(Rarely are URLs "fun" -- Honestly, I could have answered your first post much faster had I not been laughing so hard.)
Note that in the patterns, "[^-/]+" should be used preceding an expected hyphen, and "[^./]+" should be used preceding an expected period. The rules will run quite a bit faster with the correct subpatterns, as they allow parsing the requested URL-path in a single left-to-right pass, rather than repeated "fit and try" attempts.
So, for example, that makes your last rule:
RewriteRule ^herbed/hedgehog/([^-/]+-)+([^-/]+-[^-/]+-[^-/]+-[^-/]+-[^-/]+-[^-/]+-[^./]+)\.html$ /herbed/hedgehog/$2.html
And actually, if the desired behaviour is "for number sequences of 2 or more parts discard only the first part"
then only two rules are needed:
RewriteRule ^herbed/hedgehog/[^-/]+-([^./]+)\.html$ /herbed/hedgehog/$1.html
RewriteRule ^herbed/hedgehog/[^-/]+-[b](([^-/]+-)+[/b][^./]+)\.html$ /herbed/hedgehog/[b]$1[/b].html
Jim