URL Rewrite Regex in htaccess

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

URL Rewrite Regex in htaccess

Post 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.
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

FYI, I think that link is the Useful Resources sticky in this forum. If it isn't, perhaps you can add it?
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post by ska »

Hi Everah, yeah link is there already. I should have looked sooner, eh..! Cheers.
Post Reply