Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

How to redirect using wildcards in Apache

In which a n00b reveals his n00bishness



3:47 pm on Nov 25, 2002 (gmt 0)

Inactive Member
Account Expired


In trying to find or figure out the answer for myself, I've RTFM (I own and have actually read the O'Reilly book), I've RTFApacheDocs, and I've trolled through the first hundred or so of the 500+ threads here on 301ís. I still need help.

Where I have the problem is that I don't understand the meanings of all the carets, parens, backslashes, brackets, braces, bangs and stuff. I finally found out the term (I think) is Regular Expressions, (took me days just to find that out because I missed it the first time through the docs). Fine. At Amazon I found a book that (foolishly) claims to be able to help me master them. But it won't be until after the holidays that I can afford a $30 book to help with one specific issue I have right now.

So, I'm asking for help here.

I need to change some directory and filenames, sending a 301 back to the UserAgent in case it's a robot (very important).

How do I say:

1) everything in old-directory is now in new-directory under the same filename.
2) For every file in this-directory (and in a separate example, in this-domain) with .htm or .html extension, use the same filename but with the .shtml extension.
3) Both rules together (old-directory/filename.htm and .html is now new-directory/filename.shtml)

There must be a far more efficient way to do this than endless hard-coded RedirectPermanent ...

Any suggestions? (Please operate under the assumption I'm completely clueless of all symbology beyond letters and numbers, because that would be very near to the truth.)

Eternal gratitude in advance.

7:42 pm on Nov 25, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 21, 1999
votes: 0

Welcome to WebmasterWorld brucew! You've come to the right place! :)

I'm confident that one of our mod_rewrite gurus will soon post a solution for you. Until then, here's a few resources that may help you understand whatever solutions they suggest. Check out this regular expressions tutorial [gnosis.cx] and this doc [httpd.apache.org], written by the man that wrote the Apache mod_rewrite module, that offers many "real world" examples of mod_rewrite in action.

Again, welcome! :)


10:32 pm on Nov 25, 2002 (gmt 0)

Inactive Member
Account Expired


Ah. Thanks Dave. For both the welcome and the resource.

Sometimes you have a programming problem and it seems like the best solution is to use regular expressions; now you have two problems.

My situation exactly! :)

The first refernce I might get through. The second one I've already ready and reread. I don't feel bad about not understanding it since in the second 'graph it says,

mod_rewrite's major drawback is that it is not easy to understand and use for the beginner.

That would be me.

[edited by: brucew at 10:55 pm (utc) on Nov. 25, 2002]

10:52 pm on Nov 25, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0


Welcome to WebmasterWorld!

Rewrite: old-directory/filename.htm or .html to new-directory/filename.shtml

This should work...

In .htaccess :
RewriteRule ^old-directory/(.*)\.html?$ /new-directory/$1.shtml [R=301,L]

Or in httpd.conf :
RewriteRule ^/old-directory/(.*)\.html?$ /new-directory/$1.shtml [R=301,L]

Fairly efficient, I'd say... :)



11:00 pm on Nov 25, 2002 (gmt 0)

Inactive Member
Account Expired


Thanks, jd.

I shall give it a whirl presently. Giving it a quick look-see, I think there's a glimmer of understanding there.

Thanks again!