Forum Moderators: phranque

Message Too Old, No Replies

.htaccess (new additions to!)

regexp ooh err!

         

phill2000star

12:56 am on Nov 22, 2007 (gmt 0)

10+ Year Member



Hi everyone.

So far I have a htaccess in certain subdirectorys of a website I'm re-designing, and I am trying to write my own regexp for a section.

The product codes the client uses are in a particular format. They consist of 4 letters followed by 5 numeric digits. The first two letters of each product code are ALWAYS AA. the following two letters can only be one of three possibilities. BB, DE or RS.

the following are examples of his product code format

AABB00001 = product number 00001 in category AABB
AADE92501 - product number 92501 in category AADE
AARS12341 - product number 12341 in category AARS

what we are trying to create is a re-write rule for the site's redirects and so far I am trying to no avail in getting a regexp close.

We are trying to achieve the following...

http://www.domain.com/products/AABB00001.html

gets pointed to

http://www.domain.com/view_products.php?product_id=AABB00001

I'm starting with

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^.]+)\.html$ ../view_products.php?product_id=$1 [L]

what we now want to do is expand our rule to ensure that the product code entered is in the correct format. Anything that isnt in the correct format will result in a 404 page.

I know that [aA][aA] will match the first 2 A's (case insensitive) and that \b[0-9][0-9]{4}\b will match the trailing 5 digits.

Any suggestions on how I can create the full expression?

Many thanks!

jdMorgan

1:04 am on Nov 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unless I'm missing something, that would be:

RewriteRule ^(AA(BB¦DE¦RS)[0-9]{5})\.html$ ../view_products.php?product_id=$1 [NC,L]

Note that the [NC] flag makes the pattern-match case-insensitive.

Replace the broken pipe "¦" characters with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 1:08 am (utc) on Nov. 22, 2007]

phill2000star

1:09 am on Nov 22, 2007 (gmt 0)

10+ Year Member



Hi Jim,

thanks for the advice. Am I right in thinking that exp would match anything where the first 4 characters are any character from a-zA-Z?

He wants it so it matches anything starting with aa (case insensitive), followed by either bb, de or rs (again all case insensitive), folowed by the 5 digit code.

Many thanks so far!

phill2000star

1:13 am on Nov 22, 2007 (gmt 0)

10+ Year Member



ha ha...

Sorry think I posted that last reply as you were editing your last post.

All works. Perfect! Thankyou so much

jdMorgan

1:34 am on Nov 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I edited the code after re-reading the initial discussion, which had scrolled of the screen... :)

Jim