2

UPDATED - my initial question wasn't quite correct. (apologies to all concerned)

UPDATED again - (this is not my day today..)

I need to redirect all incoming image requests for:

http://www.example.com/images/asd12catalog.jpg (there is an additional alpha character)

To:

http://www.example.com/images/as-d12.jpg  (I have added the "-")

So I need to strip out the word catalog and change the first portion of the filename to add a "-" making as-d12.jpg.

I have tried variations on:

RewriteRule ^/images/[a-z0-9]catalog.jpg$ /images/$1.jpg

But I just can't seem to get a match.

Can anyone help please?

Thanks.

2 Answers 2

3

Your attempt was very close, the only major problem being that you did not actually wrap anything in your regex as a capture group. By placing parentheses around [a-zA-Z]*[0-9]* below, it will be available in the variable $1 after the match has finished. You can then use this as you expected in your redirect URL.

RewriteRule ^/images/([a-zA-Z]{2})([a-zA-Z]{1})([0-9]*)catalog.jpg$ /images/$1-$2$3.jpg

Demo:

Regex101

Sign up to request clarification or add additional context in comments.

10 Comments

thanks but I have tested that rule here - htaccess.mwl.be - and it doesn't match?
I don't know how to use this tool, but I believe my regex to be correct.
Yes I checked it on regex101 where it worked fine - but on RegexPal it doesn't? regexpal.com This is why I always struggle with regex - one pattern works in one place but not another....
@Tony Actually neither of these test tools really matter in the end, the only thing that matters is that the redirect actually works in your production setup. That being said, does my answer work for you?
I'm afraid it doesn't work. I have other rewrites that are working so I know my server config is ok.
|
1
RewriteRule ^/?images/([a-zA-Z]{2})([a-zA-Z]{1})([0-9]+)catalog.jpg$ /images/$1-$2$3.jpg

You're not specific about the exact format of your filenames, but this will match anything followed by catalog.jpg, which will hopefully cover any requirements.

Also note that the leading / should at most be optional when matching in rewrite rules - they haven't been part of the path parsed by RewriteRule since version 1. See https://webmasters.stackexchange.com/questions/27118/when-is-the-leading-slash-needed-in-mod-rewrite-patterns

Edit: updated again for new requirement

6 Comments

Apache's own documentation uses leading forward slashes.
Then their documentation is wrong - the leading slash hasn't been part of the path parsed by RewriteRule since version 2 was released. The best thing is probably to make it optional, since that satisfies both versions, although I can't imagine many people are still running v1. I'll update my answer.
Maybe your answer works for the OP, let's see. Anyway, he just changed the requirement so you should take that into account as well.
Iain, @Tim - this is just not my day. My attention to detail is clearly lacking. I have just realised my filename has three leading alphabetic characters prior to the numeric characters. So it is formatted like this: slp10catalog.jpg needs to change to sl-p10.jpg. I feel rather stupid now. Once again - profuse apologies - you have both pointed me in the right direction - I shall go away and make it work now. I'll post up the solution when I have it. <embarrassed>
@Tony I've updated my answer - it was a simple enough change.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.