PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Oct 19, 2017 6:01 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Fri Mar 15, 2013 1:51 am 
Offline
Forum Newbie

Joined: Fri Mar 15, 2013 1:46 am
Posts: 6
Hi

I'm trying write an htaccess rewrite condition to use

Syntax: [ Download ] [ Hide ]
^\/[a-z]{1}\/[\d]+\/$


to match with

Syntax: [ Download ] [ Hide ]
http://www.domain.com/p/568/foo/bar/pagename


but not with

Syntax: [ Download ] [ Hide ]
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


Last edited by kayak2013 on Sat Mar 16, 2013 2:51 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Fri Mar 15, 2013 2:46 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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).


Top
 Profile  
 
PostPosted: Fri Mar 15, 2013 3:12 am 
Offline
Forum Newbie

Joined: Fri Mar 15, 2013 1:46 am
Posts: 6
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 :

Syntax: [ Download ] [ Hide ]
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


Top
 Profile  
 
PostPosted: Fri Mar 15, 2013 5:10 am 
Offline
Forum Newbie

Joined: Fri Mar 15, 2013 1:46 am
Posts: 6
ok - I've set up a test situation with just this in the htaccess file (the rewrite condition is removed) :
Syntax: [ Download ] [ Hide ]
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 ??


Top
 Profile  
 
PostPosted: Fri Mar 15, 2013 12:44 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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:
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.


Top
 Profile  
 
PostPosted: Fri Mar 15, 2013 4:03 pm 
Offline
Forum Newbie

Joined: Fri Mar 15, 2013 1:46 am
Posts: 6
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 !


Top
 Profile  
 
PostPosted: Fri Mar 15, 2013 5:01 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Sat Mar 16, 2013 2:52 am 
Offline
Forum Newbie

Joined: Fri Mar 15, 2013 1:46 am
Posts: 6
ok
thanks again :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group