URL Rewrite Regex in htaccess
Posted: Wed Oct 24, 2007 3:11 pm
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.
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.