Page 1 of 1

[solved] prob with simple regex

Posted: Fri Mar 15, 2013 1:51 am
by kayak2013
Hi

I'm trying write an htaccess rewrite condition to use

Code: Select all

^\/[a-z]{1}\/[\d]+\/$
to match with

Code: Select all

http://www.domain.com/p/568/foo/bar/pagename
but not with

Code: Select all

http://www.domain.com/ppp/568/foo/bar/pagename
so basically the string i need to match is "/p/568/" - just 1 lower-case letter between the first pair of slashes and any number of integers between the second pair

but ..... it's not working (obviously!) - could you help me with this ?

thanks

Re: prob with simple regex

Posted: Fri Mar 15, 2013 2:46 am
by requinix
Of course you need more than just "^\/[a-z]{1}\/[\d]+\/$". What's the rest of your .htaccess?

And did you know you can't capture parts of the URL without using parentheses? Like to hold onto the letter for later you need "([a-z])", then refer to it with $N (like $1 if it's the first grouping).

Re: prob with simple regex

Posted: Fri Mar 15, 2013 3:12 am
by kayak2013
hi - thanks for your reply

I can do the capturing of bits of the url - that's not a problem - but that bit of code is part of a rewrite condition :

Code: Select all

RewriteCond %{REQUEST_URI} ^\/[a-z]{1}\/[\d]+\/$
what I would like is just to test it against the url before using it in the htaccess file

Re: prob with simple regex

Posted: Fri Mar 15, 2013 5:10 am
by kayak2013
ok - I've set up a test situation with just this in the htaccess file (the rewrite condition is removed) :

Code: Select all

SetEnv PHP_VER 5

Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10  

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^\/([a-z]{1})\/([/d]+)\/$ test\.php?t=$1&p_id=$2 [NC]
I've tried all sorts and it just wont work with the url http://www.domain.com/p/568/foo/bar/pagename :banghead:

I get the 404 not found error message - have you got any ideas ??

Re: prob with simple regex

Posted: Fri Mar 15, 2013 12:44 pm
by requinix
You did turn the \d you had into /d, that's a problem. But mostly your expression only allows for /p/568/ with nothing after it.

Couple other comments, mostly about readability (always a good thing to aim for when dealing with regular expressions):
- You don't need to escape slashes: that's only needed for escaping them as regex delimiters but mod_rewrite doesn't use any
- {1} is redundant
- \d and [\d] mean the same thing
- The replacement URL isn't a regex
- 9/10 times you'll want the [L] flag too

Code: Select all

RewriteRule ^/?([a-z])/(\d+)/ test.php?t=$1&p_id=$2 [L,NC]
I use ^/? because depending on various circumstances the URL being matched against may or may not start with a slash.

Re: prob with simple regex

Posted: Fri Mar 15, 2013 4:03 pm
by kayak2013
arg! it's amazing how many mistakes one can make in so few characters !

it works fine now and sends the captured variables and all - excellent

many thanks for your help :D

..... now, on to the next part !

Re: prob with simple regex

Posted: Fri Mar 15, 2013 5:01 pm
by requinix
kayak2013 wrote:arg! it's amazing how many mistakes one can make in so few characters !
Welcome to the world of regular expressions :D To be fair they weren't mistakes so much as they were things you might have overcomplicated a bit.

Re: [solved] prob with simple regex

Posted: Sat Mar 16, 2013 2:52 am
by kayak2013
ok
thanks again :D