htaccess RewriteRule Pattern Matching?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

herhubby86

Thread Starter
Joined
Mar 30, 2008
Messages
27
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
 
Joined
Jul 2, 2006
Messages
379
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
 
Joined
Oct 15, 2006
Messages
211
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)?
 
Joined
Jul 2, 2006
Messages
379
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 :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top