Exclude root directory

Topics: Developer Forum, User Forum
May 20, 2010 at 5:19 PM
Hi all, I would appreciate some help with a rule. I am trying to redirect all traffic from subdomain to another domain excluding the main page. This is what I got so far: RewriteCond %{HTTP_HOST} ^(chicago.domain.com).*$ RewriteCond %{URL} ^(?!/index.cfm) RewriteRule ^(.*)$ http://www.domain.com$1 [R=301 This works great if someone would go to http://chicago.domain.com/index.cfm. but I can't figure out how to write condition to exclude when someone goes to http://chicago.domain.com/ Thanks
Coordinator
May 21, 2010 at 12:54 AM

You want to NOT redirect if the request is for the main page, is that right?

In the case that the URL path is /index.cfm, you have it covered.   What you are asking about is how to handle the case where the URL path is  /  (implicit default) .

Of course in that case you just need to use a pattern for /  , as an alternative to  /index.cfm.

RewriteCond %{HTTP_HOST} ^chicago.domain.com$
RewriteCond %{URL}  ^(?!(/index\.cfm|/))$
RewriteRule ^(.*)$  http://www.domain.com$1 [R=301]

You don't need to use a RewriteCond on %{URL}, unless you really want to. You could put that in the pattern for the RewriteRule. I find it to be simpler:

RewriteCond %{HTTP_HOST} ^chicago.domain.com$
RewriteRule ^(?!(/index\.cfm|/))(.*)$ http://www.domain.com$1 [R=301]

Also - in the future I will be able to help you better if your ini file is posted in a way that I can read it, instead of all jammed together.

 

May 21, 2010 at 9:17 PM
Edited May 21, 2010 at 9:43 PM

Thanks allot for you help and sorry about the code, didn't even realized it got formatted like that.

I tried the code and it's not working, all the pages stay where they are.  Filter is working properly so must be something in the code.  BTW I am using V1.2, maybe that has something to do with?

Just to clarify things, I want all the links to go to a different domain accept the main page which is index.cfm.  I think you got it right it's just that filter is seeing "/" in the others links such as http://chicago.domain.com/Home/ and that's why it's not redirecting it.

Thanks

 

Coordinator
May 22, 2010 at 12:34 AM

v1.2 is no longer supported; I advise you to use v2.1.

good luck.

 

May 24, 2010 at 2:37 PM

OK I have v2.1 installed and everything but the rules is still not working.  I tried different combination  but I am not able to figure it out.

Here is a log :

Mon May 24 09:33:10 - 15532 - ReadSiteConfig: line  10: RewriteRule (rule 1)  '^(?!(/index\.cfm|/))(.*)$'  'http://www.domain.com$1'   (null)
Mon May 24 09:33:10 - 15532 - ReadSiteConfig: WARNING: line $d: Rewriting to a fully-qualified URL. Do you want RedirectRule or ProxyRule?
Mon May 24 09:33:10 - 15532 - ReadSiteConfig: Done reading, found 1 rules (0 errors, 1 warnings) on 11 lines
Mon May 24 09:33:10 - 15532 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon May 24 09:33:10 - 15532 - HttpFilterProc: cfg= 0x02003ED8
Mon May 24 09:33:10 - 15532 - HttpFilterProc: SF_NOTIFY_AUTH_COMPLETE
Mon May 24 09:33:10 - 15532 - DoRewrites
Mon May 24 09:33:10 - 15532 - DoRewrites: Url (no decoding): '/Best_Sellers.cfm'
Mon May 24 09:33:10 - 15532 - EvaluateRules: depth=0
Mon May 24 09:33:10 - 15532 - EvaluateRules: Rule 1 : -1 (No match)
Mon May 24 09:33:10 - 15532 - EvaluateRules: returning 0
Mon May 24 09:33:10 - 15532 - DoRewrites: No Rewrite
Mon May 24 09:33:10 - 15532 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon May 24 09:33:10 - 15532 - HttpFilterProc: cfg= 0x02003ED8
Mon May 24 09:33:10 - 15532 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon May 24 09:33:10 - 15532 - HttpFilterProc: cfg= 0x02003ED8
Mon May 24 09:33:10 - 15532 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon May 24 09:33:10 - 15532 - HttpFilterProc: cfg= 0x02003ED8
Mon May 24 09:33:10 - 15532 - HttpFilterProc: SF_NOTIFY_LOG

Coordinator
May 24, 2010 at 2:46 PM

The error message is broken - it says $d instead of an actual line number. That's a bug; I'll get it fixed.

But the warning is still intelligible - you're rewriting to a fully-qualified URL. You probably don't want to do that.  To understand why, read the documentation, specifically the section that explains rewrites versus redirects.

http://cheeso.members.winisp.net/Iirf20Help/html/2e820208-c2eb-4d6d-a134-c63f7d41244f.htm

Coordinator
May 24, 2010 at 2:49 PM

wait, no.  You are not running the latest v2.1.  You are running an older version of IIRF.   If there ever was a problem with an error message like that, it has long since been fixed.

I advise that you read the documentation, AND use the latest v2.1 IIRF.

 

May 24, 2010 at 3:04 PM

Yeah you're right, I went do download page and got the last stable version.  Now I got v2.1 beta installed and no luck still.

Here is Log:

Mon May 24 10:00:24 -  4448 - Ionic ISAPI Rewriting Filter (IIRF) 2.1.1.21 RELEASE
Mon May 24 10:00:24 -  4448 - IIRF was built on: May 22 2010 18:06:18
Mon May 24 10:00:24 -  4448 - GetLogFile: app:'/LM/W3SVC/1173049356/Root'  new log:'c:\temp\newone8.log.8072.log'
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: actual log file 'c:\temp\newone8.log.8072.log'
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: ini file: 'D:\Sites\FF-Chicago\Iirf.ini'
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: ini file timestamp: 2010/05/24 10:00:21 Central Daylight Time
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: cfg(0x02003EF0)
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: LogLevel = 3
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: D:\Sites\FF-Chicago\Iirf.ini(3): RewriteEngine will be enabled.
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: D:\Sites\FF-Chicago\Iirf.ini(9): RewriteRule (rule 1)  '^(?!(/index\.cfm|/))(.*)$'  'http://www.domain.com$1'  [R=301]
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: INFO: RewriteRule with R modifier - will Redirect
Mon May 24 10:00:24 -  4448 - ReadVdirConfig: Done reading INI for the root vdir, found 1 rules (0 errors, 0 warnings) on 11 lines
Mon May 24 10:00:24 -  4448 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon May 24 10:00:24 -  4448 - HttpFilterProc: cfg= 0x02003EF0
Mon May 24 10:00:24 -  4448 - HttpFilterProc: SF_NOTIFY_AUTH_COMPLETE
Mon May 24 10:00:24 -  4448 - DoRewrites
Mon May 24 10:00:24 -  4448 - DoRewrites: Url (no decoding): '/Best_Sellers.cfm'
Mon May 24 10:00:24 -  4448 - EvaluateRules: depth=0
Mon May 24 10:00:24 -  4448 - EvaluateRules: no RewriteBase
Mon May 24 10:00:24 -  4448 - EvaluateRules: Rule 1: -1 (No match)
Mon May 24 10:00:24 -  4448 - EvaluateRules: returning 0
Mon May 24 10:00:24 -  4448 - DoRewrites: No Rewrite
Mon May 24 10:00:24 -  4448 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon May 24 10:00:24 -  4448 - HttpFilterProc: cfg= 0x02003EF0
Mon May 24 10:00:24 -  4448 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon May 24 10:00:24 -  4448 - HttpFilterProc: cfg= 0x02003EF0
Mon May 24 10:00:24 -  4448 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon May 24 10:00:24 -  4448 - HttpFilterProc: cfg= 0x02003EF0
Mon May 24 10:00:24 -  4448 - HttpFilterProc: SF_NOTIFY_LOG

Coordinator
May 24, 2010 at 3:08 PM
Edited May 24, 2010 at 3:09 PM

did you READ the documentation?

In my last post I suggested that you READ the documentation AND get the latest version.

It seems that you have done only one of those things.

 

May 24, 2010 at 3:32 PM

I have been reading the documentation but I still can't figure out how to set the filter to detect when URL is / (implicit default)

May 24, 2010 at 3:42 PM

I ran some code and if I use something like :

RewriteRule ^(/)$ http://www.domain.com/$1 [R=301]

All the pages do not forward except  the http://www.chicago.domain.com/    (implicit default) but if I do the opposite where default page should not forward and all others should then it doesn't work.

RewriteRule ^(?!/)$ http://www.domain.com/$1 [R=301]

 

Coordinator
May 24, 2010 at 3:49 PM

I'm havin some trouble understanding you.

When you say,

All the pages do not forward except the http://www.chicago.domain.com/

...does that mean, Only the request http://www.chicago.domain.com/ gets redirected ?

In addition to telling me what is or is not happening, can you take one step back and also tell me what you WANT to happen.?

May 24, 2010 at 4:15 PM

If url is http://chicago.domain.com/ or http://chicago.domain.com/index.cfm I want it to do nothing for everything else I want it to forward to http://www.domain.com
Doing  ?!(/index\.cfm) works just fine but it's the implicit default url (http://chicago.domain.com/) that doesn't work.

 I thought when I have RewriteRule ^(?!/)$ http://www.domain.com/$1 [R=301] everything should forward cept http://chicago.domain.com/ but it doesn't.  Everything stays as it is.

So if I do http://chicago.domain.com/aboutus.cfm nothing happens and log file says: Rule 1: -1 (No match) which tells me that it actually found "/" in the url so it's not going to do anything, right?

 

 

Coordinator
May 24, 2010 at 4:26 PM

When you say "forward" do you mean "Redirect"?   "Rewrite"?

 

Coordinator
May 24, 2010 at 4:28 PM

Also, you wrote "for everything else I want it to forward to http://www.domain.com" - does that imply, http://www.domain.com/ should redirect to http://www.domain.com  - an infinite redirect loop?

 

May 24, 2010 at 4:52 PM

What I mean exactly is Redirect.

I have two sites chicago.domain.com and domain.com.  I want users to be able to go to http://chicago.domain.com or http://chicago.domain.com/ or http://chicago.domain.com/index.cfm and not get redirected but if they click on any of the links on that site I want it to redirect to http://www.domain.com/$1

Since http://www.domain.com is separate site it's not going to get affected by ISAPI filter so there will be no infinite loops or anything else like that.  Does that make more sense?

 

May 24, 2010 at 5:14 PM

Here is a dirty fix that I did to accomplish what I need

RewriteRule ^(/)$ http://chicago.domain.com/index.cfm [R=301]
RewriteRule ^(?!/index\.cfm)(.*)$ http://www.domain.com/$1 [R=301]

Coordinator
May 24, 2010 at 5:21 PM

That doesn't seem like a very dirty fix, to me.

It seems pretty clean.

I guess there's one additional thing you might want to do, and that's insert a RewriteCond that tests for the hostname, so that you don't redirect if the hostname is already www.domain.com.

 

 

May 24, 2010 at 6:03 PM
Edited May 24, 2010 at 8:23 PM

Any reason why ^(?!/)$ doesn't work?

Thanks for your help!

Coordinator
May 25, 2010 at 12:13 AM

^(?!/)$ is a regex that says "beginning-of-line followed by end-of-line. 

All URLs in IIS include the leading slash.  So, that regex will nevevr match anything. (Unless you use RewriteBase in a surprising way).

The (?!/) is a zero-width non-capturing negative lookahead assertion.  Since your regex is empty (^$) without that zero-width assertion, the assertion itself is meaningless.