Redirect any paths but not main page

Jul 31, 2009 at 5:12 PM

I was wondering if this would be possible because we moved the website to a new server:

Old URL:

New URL:

so when a user tries they would get a static page untouched. However if they user enters I would like to redirect them to How would the syntax within the ini file look for this?

RewriteRule ^/(.*)$$1   [R]


RedirectRule ^/(.*)$$1   [R]


I also need to make sure anyone accessing does not get redirected however.

Thanks for the help!

Jul 31, 2009 at 5:13 PM

I believe this is the solution but is there a better way?


RedirectRule ^/old(.*)$$1   [R=302]

Jul 31, 2009 at 6:58 PM

Thanks for using the forums.

You have two goals: one is that all requests to the "bare url"  should be served some static page, I guess a page that you set up as the default.  To insure this happens, you can do something like this:

  RewriteCond   %{HTTP_HOST}   ^main\.com$
  RewriteRule   ^/?$      -  [L]

This says, if a request arrives, using as the HTTP_HOST name, and it is a bare url (either / or nothing), then don't rewrite it at all (that's what the - does), and make this the Last rule processed ([L]).

That takes care of goal #1. The second goal is to redirect the old URLs to the new URL space, and new domain.

  RewriteCond   %{HTTP_HOST}   ^main\.com$
  RedirectRule   ^/old(.*)$$1 [L,R=301]

I think you want R=301, which means "moved permanently." 302 implies "moved temporarily" or something else. From your description, 302 probably isn't what you want.

These rules will not apply if someone visits, because the HTTP_HOST is matched against (no prefix).

You asked whether to use RewriteRule or RedirectRule. This may sound obvious, but RewriteRule is for Rewrites, and RedirectRule is for Redirects. If you don't understand the difference, check the doc.

putting it all together:

  RewriteLog c:\temp\IIRF
  RewriteLogLevel 2

  StatusUrl  /iirfstatus

  # don't rewrite or redirect requests like
  RewriteCond   %{HTTP_HOST}   ^main\.com$
  RewriteRule   ^/?$      -  [L]

  # Redirect other requests on to
  RewriteCond   %{HTTP_HOST}   ^main\.com$
  RedirectRule   ^/old(.*)$$1 [L,R=301]

Before deploying this solution, I suggest you check out the test driver to verify that the URL requests you expect to receive are in fact transformed in the way you expect.


Aug 1, 2009 at 5:14 AM

So how could we also include the www prefix?

Aug 1, 2009 at 7:16 AM

How about maybe replacing ^main\.com$ with ^www\.main\.com$


I must not be understanding something, it seems too simple.

Aug 1, 2009 at 10:07 AM

I think S4iNT wants that this rule will be apply in both cases: OR So pattern may looks like ^(?:www\.)?main\.com$

As i can see, vast majority of the issues on this forum related to regular expressions itself, actually regardless to IIRF.  Disappointed, but predictable situation :)

Aug 1, 2009 at 1:24 PM

hmm, ok. I think what you want is something like this:

  # don't rewrite or redirect requests like or
  RewriteCond   %{HTTP_HOST}   ^(www\.)?main\.com$ 
  RewriteRule   ^/?$      -  [L]

The (www\.) matches www. The succeeding question-mark is a quantifier that says "zero or more of those".

If you like you can chain two RewriteCond statements like so:

  # don't rewrite or redirect requests like or
  RewriteCond   %{HTTP_HOST}   ^www\.main\.com$  [OR]
  RewriteCond   %{HTTP_HOST}   ^main\.com$
  RewriteRule   ^/?$      -  [L]

The chained pair of RewriteCond statements is equivalent to using a single RewriteCond with the optional www in it.  Notice the [OR] - that is the logical conjunction.   When chaining RewriteCond statements, [AND] is also supported, or is implicitly used in the absence of a conjunction. In this case you want [OR].

I know Regex can be an obscure science. It's an entirely new language.  The IIRF ini file also conforms to a sort of language.  It takes some time to figure out how to use Regex, and also how to use IIRF ini files. Regex in particular are sort of a mixed bag. So powerful and useful, but tricky to get right.  Winston Churchill once said,  "Democracy is the worst form of government except for all those others that have been tried."   Regex are like that, too.  They are the worst form of pattern matching language, except all the others that have been tried.

I try to make the concept as approachable as I can. The .chm file has many examples with extended english explanations of various regex and how they work, piece-by-piece.  There is an example almost exactly like the one we are discussing here, in the RewriteCond entry - scroll about 40% down: .


Aug 3, 2009 at 12:26 PM

I know I tried using the chm help file but all the topics came back page cannot be displayed. However, with the following statement I had two quick questions:

1. Using rewriterule instead fo redirect for the first condition. Why would we not use redirect? Is this because we don't really need to do anythign because we want to keep them at that url? and a redirect would actually still redirect the user and is a noticiable change for the user while the rewrite is backend and is not visiable to the user?

2. I am currently only useing one line in my ini file:
RedirectRule   ^/old(.*)$$1 [R]

and I am acheiving desired results for does not get redirected and only* gets redirected to* Is there any reason why conditions need to be set when the function calls appear to hand the condition already or this using the ini file below just that much more stable?

# don't rewrite or redirect requests like or
  RewriteCond   %{HTTP_HOST}   ^www\.main\.com$  [OR]
  RewriteCond   %{HTTP_HOST}   ^main\.com$
  RewriteRule   ^/?$      -  [L]

  # Redirect other requests on to
  RewriteCond   %{HTTP_HOST}   ^main\.com$
  RedirectRule   ^/old(.*)$$1 [L,R=301]

Thanks again for all the help guys! I was looking into writing my own ISAPI filter however my main experience has been admin scripts with vbs, C# and j# and this ISAPI filter appears to be pretty stable so why reinvent the wheel right?

Aug 3, 2009 at 6:56 PM

ok, first, I suggest you need to read the help file.  From the FAQ on the front page:

Where can I find the documentation for IIRF?
There's a CHM file that provides all the doc. It describes the features and how to use them. You can also view the helpfile online

I cannot view the CHM documentation. The error is: "This program cannot display the webpage." or, "The address is invalid." Is there an error with the chm file?
No, there is no problem with the CHM file. You have likely encountered a problem with Windows protection of files downloaded from less trusted locations. To view the chm file, within Windows Explorer, right-click on the CHM file, select properties, and Unblock it (button in lower part of properties window).

The helpfile explains many questions you have, and some you ought to have but haven't thought of. It explains rewrite versus redirect. It's your choice which one you want to use.  But if you want a redirect, use RedirectRule.  If you want a Rewrite, use RewriteRule.   The help file also provides example rules and it dissects regex's for common URL parsing tasks. It's indispensable if you want to use IIRF.  It explains why RewriteCond is used. 

Seriously.  Read it.