Page 1 of 1

URL Rewrite Regex in htaccess

Posted: Wed Oct 24, 2007 3:11 pm
by ska
Hi guys,

I have a query for a specific regex I'm writing in an htaccess file for a URL Rewrite. I've got most of them working, it's just the last two;

#this ones rewrites for example /admin/ to index.php?target=admin
RewriteRule ^([^/\.]+)/$ index.php?target=$1 [L]

#this one rewrites for example news/1234-something-something.html to index.php?target=viewarticle&articleid=1234
RewriteRule ^news/([^/\.]+)-([^/\.]+).html$ index.php?target=viewarticle&article_id=$1 [L]

#this one SHOULD rewrite somecategory/someothercategory/productname_p1234.html to index.php?target=viewproduct&prodid=$3
RewriteRule ^([^/\.]+)/([^/\.]+)_p([^/\.]+).html$ index.php?target=viewproduct&prodid=$3 [L]

#this one SHOULD rewrite somecategory/someothercategory/categoryname_c1234.html to index.php?target= viewcategory&prodid=$3
RewriteRule ^([^/\.]+)/([^/\.]+)_c([^/\.]+).html$ index.php?target=viewcategory&catid=$2 [L]

So my question really is how to accomodate for an unknown directory level in the URL before the file name, as I know I can get it to work with the second example for instance. How do you write /somedirectory/someotherdirectory/etc/etc/ into a regex? I don't care what they are, all I want is the bit after the c or p letter in the filename.

-- EDIT --
Just to reword the query even more concisely, what is the correct regex to identify a url with an unknown directory level, where the filename at the end is the bit I'm interested in? For example;

dir1/dir2/dir3/....dirx/somefilename_c1234.html

-- END EDIT --

Any pointers would be appreciated.

Thanks.

Posted: Thu Oct 25, 2007 12:09 pm
by ska
Fixed it. Just had to actually think about it...!

Code: Select all

RewriteRule ^(.*)/([^/\.]+)_c([^/\.]+).html$ index.php?target=viewcategory&catid=$3 [L]
i.e. (.*) represents as many directories in the url as nescessary.

This is useful;


http://www.ilovejackdaniels.com/mod_rew ... _sheet.png

Posted: Thu Oct 25, 2007 4:19 pm
by RobertGonzalez
FYI, I think that link is the Useful Resources sticky in this forum. If it isn't, perhaps you can add it?

Posted: Fri Oct 26, 2007 4:37 am
by ska
Hi Everah, yeah link is there already. I should have looked sooner, eh..! Cheers.