Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule help

Matching first three characters in filename

         

cookie2

12:49 am on Mar 14, 2014 (gmt 0)

10+ Year Member



I'm going nuts trying to get this regex for an htaccess rewrite to work. I need to redirect file requests that contain RS= in that order. I've tried:

RewriteRule ^([RS]{2}=)$ http://mydomain.com/index.html


RewriteRule ^([RS=]{3})$ http://mydomain.com/index.html


RewriteRule ^/([RS=]{3})$ http://mydomain.com/index.html


and several others but nothing works. I've googled to see if I could find any better answers on how to match the first three characters in a file name but noting I tried has worked.
Can someone please offer me a suggestion as to what to do or where to look? Thanks.

wilderness

1:24 am on Mar 14, 2014 (gmt 0)

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



explore this

[a-z]{2}

cookie2

2:05 am on Mar 14, 2014 (gmt 0)

10+ Year Member



Thanks for the suggestion but I only want to match the ones that start with RS=. What you show would also match RC or RA etc. and doesn't account for the = sign. I need to match RS= and only RS=.

wilderness

2:41 am on Mar 14, 2014 (gmt 0)

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



^RS=

g1smd

5:39 am on Mar 14, 2014 (gmt 0)

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



Are these in the path part of the URL request or in an attached query string?

Unencoded 'equals' isn't valid in the path part of a URL. It is reserved as the separator between parameter name and parameter value in query strings

[edited by: g1smd at 5:42 am (utc) on Mar 14, 2014]

lucy24

5:42 am on Mar 14, 2014 (gmt 0)

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



I need to redirect file requests that contain RS= in that order. I've tried:

RewriteRule ^([RS]{2}=)

What's with the parentheses and brackets? If you're matching literal text, a simple
^RS=

will do. The form
[RS]{2}
is only necessary if you need to match all of:
RR
RS
SR
SS

You don't need parentheses if you're not capturing for reuse. And you DEFINITELY don't need a closing anchor, since that would only match requests for
"www.example.com/RS="
exactly and that's all.

No leading / slash. That's only for rules lying loose in your config file, and I assume this is happening in htaccess.

(Equals signs in your URLs? Really? I hope you're redirecting in order to get rid of them!)

Oh, and, ahem, the target is
http://www.example.com/

and that's all. No "index.html". I hope you've got the appropriate [R=301,L] flags; hard to tell if that was just a selective cut-and-paste.

Edit: The body of your post said "contain" but the subject header said "first three characters" so I assumed that was what you meant.

Further edit: I'm overlapping with g1. If = signs are flatly forbidden in paths, then obviously there's something more to the question.

cookie2

1:34 pm on Mar 14, 2014 (gmt 0)

10+ Year Member



The RS= are in the path. It isn't in my URL. It's part of the filename being requested and I don't know where it's coming from. Example request:
/blogs/myblog/guest_bloggers/RS=^ADAzyT7tVsrGwbLCZ_NFz9.1eROKp8

I want to capture all of those requests that start with RS= and send them to the index page. Yes, this is in htaccess.

g1smd

1:56 pm on Mar 14, 2014 (gmt 0)

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



I think an infinite number of redirects to the home page is a bad idea.
You'd be better off blocking access and serving a useful error message.

cookie2

2:05 pm on Mar 14, 2014 (gmt 0)

10+ Year Member



But how, g1smd? The string of characters after RS= varies. It's not the same on all requests.

g1smd

3:20 pm on Mar 14, 2014 (gmt 0)

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



The RegEx pattern ^RS means "begins with RS".

A RewriteRule with the F or G flag may be useful.

cookie2

4:09 pm on Mar 14, 2014 (gmt 0)

10+ Year Member



When I use
RewriteRule ^RS= http://example.com/index.html [R=301,L]
it doesn't match and site shows a 404 page for
/blogs/myblog/guest_bloggers/RS=^ADAzyT7tVsrGwbLCZ_NFz9.1eROKp8

But when I use an online regex generator using only the RS=^ADAzyT7tVsrGwbLCZ_NFz9.1eROKp8 part then it matches. I must be missing something but I don't know what.

g1smd

6:07 pm on Mar 14, 2014 (gmt 0)

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



Escape the = sign as \= and add the rest of the path.

RewriteRule ^blogs/myblog/guest_bloggers/RS\= - [G]

lucy24

8:51 pm on Mar 14, 2014 (gmt 0)

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



RewriteRule ^RS= http://example.com/index.html [R=301,L]
it doesn't match and site shows a 404 page for
/blogs/myblog/guest_bloggers/RS=^ADAzyT7tVsrGwbLCZ_NFz9.1eROKp8

Well, of course it doesn't match. The pattern says "path must begin with 'RS=' and instead the 'RS=' element comes much further along in the path.

:: lightbulb ::

I think I see the problem. You're thinking of FilesMatch, where you're matching against the name of the file. In mod_rewrite you're matching the entire path, based on where the htaccess is located. To match "filename begins with..." the most universal form would be

(^|/)RS=

If you're not capturing, you don't need to worry about what-if-anything came before the filename. But if the path is always the same (the /blogs/ blahblah part) then spell it out explicitly.

g1, why do equals signs have to be escaped?

cookie2

10:20 pm on Mar 14, 2014 (gmt 0)

10+ Year Member



Played around with both of your suggestions. Discovered that if I added to htaccess in /root or /blogs it didn't work. But I tried putting a new htaccess in /blog/myblog and the (^|/)RS= one from Lucy worked.

Thank you both for putting me on the right track. Your help is much appreciated.