Forum Moderators: phranque

Message Too Old, No Replies

wildcard on RewriteRule for beginning of string

         

Dan99

4:31 pm on Oct 14, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Excuse the newbie question. Can someone help me produce a RewriteRule with a wildcard that directs any sort of *letter.txt to another file. That is, when a RewriteCond identifies a request from a particuar referrer for joeletter.txt, or sarahletter.txt, or dinosaurletter.txt, or 12345garbage.txt, I want them to get shunted to betterletter.txt. I think I know how to write a widcard for a string *beginning* with a substring, but I'm not quite sure how to do it for a string ending with a substring.

lucy24

6:36 pm on Oct 14, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



All you have to do is omit the opening anchor. No wild card needed, since you're not capturing anything. You will also need a RewriteCond that excludes "betterletter.txt".

Dan99

6:46 pm on Oct 14, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Wait, so you mean that "RewriteRule ^letter.txt" will capture letter.txt and anythingletter.txt?

lucy24

8:29 pm on Oct 14, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, other way around. You need a closing anchor and no opening anchor.

Dan99

8:38 pm on Oct 14, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hmm. So just "letter.txt$" will do it?

lucy24

10:25 pm on Oct 14, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes. Strictly speaking it should be
letter\.txt$
with escaped literal . period, but this is a non-lethal error.

Don't forget the RewriteCond, or you will have an infinite loop. I don't know for sure one way or the other, but I am inclined to think that
!betterletter\.txt
as a Condition would be easier on the server than a lookback
(?<!better)letter\.txt
in the body of the rule.

Dan99

10:46 pm on Oct 14, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you!
Yes, the RewriteCond exclusion is not forgotten, as I did the escaped literal. That's important. Agree about !betterletter\.txt.