# htaccess RewriteRule Pattern Matching?



## herhubby86 (Mar 31, 2008)

I recently saved a bunch of JPG photos in the root directory of my web site, and would like to move them to a subfolder to clean things up. Right now I'm handling each file one at a time in my htaccess file:

RewriteRule ^rabbit01\.jpg rabbit/rabbit01.jpg
RewriteRule ^rabbit02\.jpg rabbit/rabbit02.jpg
RewriteRule ^rabbit03\.jpg rabbit/rabbit03.jpg

Unfortunately, with over 30 images this makes for a cluttered htaccess file. Is there a way to substitute a wildcard of some type in a single RewriteRule, rather than have 30+ lines dedicated to this?

I have other JPG's in the root directory that I do NOT want to move.


On a related note, I recently rewrote my HTM web pages as PHP pages. As with the above example, I'm handling them one at a time like this:

RewriteRule ^automotive\.htm automotive.php
RewriteRule ^business\.htm business.php

It would be nice to do this with wildcard pattern matching (rewrite all HTM pages as PHP pages), but I DO NOT want to rewrite pages in subdirectories:

newsletters\2010_12.htm
newsletters\2010_11.htm

For now I am rewriting the pages rather than redirecting as things are in transition. But, is there a way to Redirect HTM pages in the root directory (only) of my web site to their new PHP equivalents?

Thanks,

Anthony


----------



## andythepandy (Jul 2, 2006)

For the first part, you could use:

RewriteRule ^(rabbit[0-9]{2}\.jpg) rabbit/$1

This will match anything with the word rabbit followed by two numbers. The {2} part tells it how many numbers to look for, you could just use [0-9]+ because that would match rabbit followed by one or more numbers.

For your second problem, try this:

RewriteRule ^([^\\\/.]*)\.htm $1.php

That will match anything without forward or back slashes in, so it will only match files in the current folder

Hope that helps
Andy


----------



## herhubby86 (Mar 31, 2008)

Andy,

Excellent! Thank you, both RewriteRule's work perfectly.

Anthony


----------



## thumb10.40 (Oct 15, 2006)

I hope you guys don't mind me asking a question in your thread but I was reading through it and although I understand the rewrite rules for the most part, I'm trying to figure out what on earth the dot is doing inside the negated character class. Would you mind helping me understand this Andy (or anyone who knows)?


----------



## andythepandy (Jul 2, 2006)

Well, I don't think that was meant to be there! All that character class is meant to do is stop forward or black slashes so I think that dot may have slipped in accidentally


----------

