.htaccess Regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

.htaccess Regex

Post by superdezign »

Why doesn't .htaccess handle regex correctly? It can't compile this:

Code: Select all

^([^/\?]+?)(/|/?\?[^/]+)?$
Before, I basically hacked my way to making a "valid" regex for my website to be barely functional. However, I can't get it to handle regex correctly. Locally, the regex works fine, but not on my host's server. They have Apache 1.3 (I have Apache 2), so does that have to do with it?

Is anyone aware of what regex .htaccess can handle, and what it can't?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I'm interested in this as well. I have seen regex on one box cause internal server errors on another. Never really looked into it though.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I've made an important advancement here. .htaccess (at least on this server) does not support optional subpatterns (? modifier on subpatterns). That's horrible, because that means I have to write out all of the different options.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I believe the issue I had was with a ? as well.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Yeah, I can't even get (.*?)/ to work.

I've come up with a solution though. I'm writing a quick little URL parser class in PHP and rewriting the URL to just go to the page without sending any parameters. Then, in PHP, regex $_SERVER['REQUEST_URI']. This also preserves other GET variables in the URL (which was the reason I was trying to change my regex in the first place).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Last I saw, Apache used POSIX regex, not PCRE.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Yup, if you take a look at http://httpd.apache.org/docs/2.0/new_features_2_0.html they briefly mention the upgrade.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Ugh. 1.3 is such a waste of space.

I specifically request to be on their PHP5/MySQL 5 server, and it still uses Apache 1.3....
Post Reply