1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

htaccess RewriteRule Pattern Matching?

Discussion in 'Web Design & Development' started by herhubby86, Jan 21, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. herhubby86

    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
     
  2. andythepandy

    andythepandy

    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
     
  3. herhubby86

    herhubby86 Thread Starter

    Joined:
    Mar 30, 2008
    Messages:
    27
    Andy,

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

    Anthony
     
  4. thumb10.40

    thumb10.40

    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)?
     
  5. andythepandy

    andythepandy

    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 :)
     
  6. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/976044

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice