Page 1 of 1

Testing for a 'complete' string.

Posted: Tue Feb 13, 2007 6:24 pm
by seodevhead
I am doing a mod_rewrite and need to create a pattern that excludes certain strings from the URL, however, I am unsure how to write the pattern so that it only disallows the entire string, not letters within:

Example:
- I need to exclude the words member and user from the URL in a specific spot.

Code: Select all

RewriteRule ^([^member|user]+)/([^/\.]+)-([^/\.]+)/?$ page.php?r=$3 [L]
* Please ignore the "anything goes" patterns above, as they are simply put there for simpleness sake.

The Rule I have created above does not work because it does not only exclude the entire "member" and "user" words, but it also excludes anything that includes one of the letters listed above.

How can I get this first part of the pattern to only exclude the words member and user in whole? Not parts of. Thanks for your help and guidance on this.

Posted: Tue Feb 13, 2007 7:11 pm
by feyd
(?!member|user) possibly. [] are character classes.

Posted: Tue Feb 13, 2007 7:46 pm
by seodevhead
feyd wrote:(?!member|user) possibly. [] are character classes.
Nah... still not doing the trick. :(

So [] won't work either way, cause they are character classes? Hmmm... this is a stumper.

Posted: Fri Feb 16, 2007 12:00 am
by Superman859
Within a character class, | has no meaning - it is simply another character and not a metacharacter.

So how can we come up with a method that doesn't use alternation?

I'm surprised the negative lookahead didn't do the trick...How did you construct it?

^(?!member|user)[^/\.]+/([^/\.]+)-([^/\.]+)/?$

That should make sure that it starts at the beginning, looks ahead and DOESN'T find 'member' or 'user' there, and then continue to match. The [^/]+ would be whatever would be that first directory. You could capture it also if you wanted.

^(?!member|user)([^/\.]+)/([^/\.]+)-([^/\.]+)/?$

What happens when you use this? Does it fail the match? Match incorrectly?

Posted: Fri Feb 16, 2007 6:30 am
by seodevhead
Hey Superman,

Thanks for the assistance and advice. I am still getting a 500 Internal Server Error. After researching the negative lookahead, it seems that it should work, but for some reason, it isn't. Here is the exact rewriterule I am using... of course, I've tinkered with it a ton as well with the same results :(

RewriteRule ^(?!member|user)[^/\.]?/([^/\.]+)-([^/\.]+)/?$ page.php?r=$3 [L]

The funny thing is, when I take out the ?! lookahead, the 500 Internal Server Error goes away. And the pattern successfully accepts any URL that starts with either 'member' or 'user'. The moment I add back the ?! lookahead, it gives the 500 error.

So as you can see my brain is sizzling as to what is going on here. To my knowledge, the lookahead should be working. Any ideas on how I can get this working? I really appreciate any help and assistance you can provide. Thanks again.

Posted: Fri Feb 16, 2007 7:46 am
by Superman859
Alright - I will look into it this afternoon.

Can you post a couple URLs that you are trying to match, so that I can mess around with exactly the same structure you are trying to match?

Posted: Fri Feb 16, 2007 11:56 am
by seodevhead
Superman859 wrote:Alright - I will look into it this afternoon. Can you post a couple URLs that you are trying to match, so that I can mess around with exactly the same structure you are trying to match?
Boy.. thank you so much for your willingness to help me out. I really appreciate it.

Here's an example of a URL I am trying to match:

Code: Select all

http://www.example.com/folder/file-name-12/
Basically, where you see "folder", I want it to match ANYTHING except the strings 'member' and 'user'. Of course, I don't want to accept forward-slashes and things like that... for simplicity sake, I just want to allow anything except the strings 'member' and 'user'.

Thanks again for all your help.

Posted: Fri Feb 16, 2007 1:29 pm
by Superman859
Try this:

(?!member|user)([^/\.]+)/([^/\.]+)/?$


That should match anything in the form folder/something/ I would think.

Although I'm unsure of what a mod_rewrite is (still new to a lot of terms!). Because the $ is included, it should only match back to something/something/ and not any further (such as something/something/something). Is the file-name a directory, or would you want to match something like folder/something.php. If that's the case, you would need to allow for the period.

If you need to use the hyphens to separate it for backtracking, and it's always the same double hyphen filename structure, you could use

(?!member|user)([^/\.]+)/([^/\.-]+-([^/\.-]+-([^/\.-]+)/?$

that should match folder/file-name-12/ and also capture folder, file, name, and 12. You could split it up however you wanted, depending on the use.

But I'm thinking that it might not be matching if you used the ^ to start off if the line begins with http:// - I'm pretty sure ^ matches the start of the line, meaning what you are trying to apply the regex on would need to be in the form folder/something/ instead of http://www.example.com/folder/something/

Although now that I re-read your post, you say everything worked fine without the negative lookahead. Hmm..

I'm not sure! Perhaps using a conditional?

(?member dosomething|else) . I'm really not sure..I need to brush up on my regex.

Is alternation allowed in a negative lookahead? Perhaps split it up and see what happens...

instead of (?!member|user), try (?!member)(?!user)....

I wouldn't think it would be a problem, but I don't know!

AH HA! I REMEMBER! maybe.

Yes, yes...try splitting it up. If you are doing this in PHP, which is basically the same thing as Perl, then you may not be allowed to use:

(?!member|user)

This is because it is not a fixed length. The length can either be 6 characters or 4, and Perl is pretty restrictive with the length allowed in a lookaround. However, PHP might allow for variable length, but I'm not entirely sure...Oh but then again the restriction may only apply to a lookbehind and not a lookahead...

Try it?

Posted: Sat Feb 17, 2007 2:47 am
by GeertDD
Superman859 wrote:Perhaps using a conditional?

(?member dosomething|else) . I'm really not sure..I need to brush up on my regex.
What about using a negated RewriteCond that checks for the presence of the words member/user and then applying the RewriteRule?
Is alternation allowed in a negative lookahead? Perhaps split it up and see what happens...
Lookaheads, as far as I know, have no restrictions. Lookbehinds do.