Mod_rewrite problems

Associate
Joined
30 Dec 2005
Posts
415
Greetings all!

I'm just writing a mod_rewrite rule, but wanted to get a few pointers...

For $1 of the URL, it needs to match underscores, all letters (regardless of case) and all numbers.
For $2 of the URL, it needs to match $ - _ . + ! * ' ( ) , all letters (regardless of case) and all numbers.

This is what I have so far:
Code:
RewriteRule ^zones/([_a-zA-Z0-9-]+)/things/(['_a-zA-Z0-9-]+)/discussion/$ things/discussion_newtopic.php?z=$1&t=$2 [NC]

Have I done this right? How would I add the special characters - Just escape them with a backslash?

Cheers all!
 
Cheers guys!

I had a go at writing it, and in my RegExp tester (software based), and this works fine:
Code:
^[a-zA-Z0-9 '\(\)-]*$|
Running it by the string
Code:
hello world '()- gf hgfh fgh

However, when I put this in the PHP, PHP kicks up an error:
Warning: preg_match() [function.preg-match]: No ending delimiter '^' found
So I added a | to the front of the regexp, and it runs fine, but doesn't match the string :(
 
regular expressions are pure evil. i just use

(.+)

that matches anything. worry about cleansing the $_GET values in your script. :)

edit: this is probably awful advice, so ignore me. :p
 
Cheers guys!

I had a go at writing it, and in my RegExp tester (software based), and this works fine:
Code:
^[a-zA-Z0-9 '\(\)-]*$|
Running it by the string
Code:
hello world '()- gf hgfh fgh

However, when I put this in the PHP, PHP kicks up an error:

So I added a | to the front of the regexp, and it runs fine, but doesn't match the string :(

that's because preg replace uses perl format regular expressions, whereas mod_rewrite uses posix (I think, it's a different format to preg anyway)

anyway, using preg_replace, you need to use a beginning and end delimiter. I normally use backticks, so a blank preg_replace would look like:

Code:
preg_replace('`[A-Z]*`','blah',$subject);

marc - regular expressions are INCREDIBLY useful. they're definitely worth learning to at least a basic standard (I still refer to manuals most of the time!)
 
marc - regular expressions are INCREDIBLY useful. they're definitely worth learning to at least a basic standard (I still refer to manuals most of the time!)

absolutely, i can see how they would be useful in scripts/other programming.

but i can't see the point for a simple mod_rewrite. :o
 
Back
Top Bottom