RewriteCond works with RedirectRule, but not with RewriteRule

Topics: Developer Forum, User Forum
Nov 3, 2009 at 5:56 AM

Hi,

I was trying to rewrite so that any request to property.uushop.com will be served by http://ushop.com/property/ . Unfortunately only redirect works but not rewrite.

How to make rewrite to work?

RewriteCond %{SERVER_NAME} ^property\.ushop\.com$   [I]
#RewriteRule ^/(.*)$ http://ushop.com/property/$1 [R]
RedirectRule ^/(.*)$ http://ushop.com/property/$1 [R]

Thanks

Eric

Nov 3, 2009 at 8:22 AM

RewriteCond %{SERVER_NAME} ^property\.ushop\.com$   [I]
RewriteRule ^/(.*)$ /property/$1 [R]

Cheeso has written a very comprehensive documentation. You should check it out.

Nov 3, 2009 at 8:34 AM

Thanks, but I require any http://(www)?.property.ushop.com/$1 to rewrite to http://ushop.com/property/$1

It didn't work. is something amiss?

 

Nov 3, 2009 at 8:40 AM

How about dropping in a wildcard before the property.ushop.com domain?

Something like (haven't tested this)

RewriteCond %{SERVER_NAME} ^[^\.]\.?property\.ushop\.com$   [I]

Try running this through the testing tool to see which URLs are completing and which are not.

I presume you're trying to get anything.property.ushop.com?qs as well as just property.ushop.com?qs to ushop.com/property/qs

Does that sound right?

Try out the example above, especially with the testing tool and see how you get on.

I'm sure Cheeso will be in soon to correct me! =)

HTH

Nov 3, 2009 at 8:50 AM

Thanks shonk,

The rewritecond works and capture the http://property.ushop.com/qs  and is redirected to http://ushop.com/property/qs  but what i want is to rewrite to http://ushop.com/property/qs  which is not working.

RewriteCond %{SERVER_NAME} ^property\.ushop\.com$   [I]
#RewriteRule ^/(.*)$ http://ushop.com/property/$1 [R]
RedirectRule ^/(.*)$ http://ushop.com/property/$1 [R]

Hopefully you or Cheeso can solve it. :)

 

Nov 3, 2009 at 8:54 AM

So when you combine my 2 answers above:

RewriteCond %{SERVER_NAME} ^[^\.]\.?property\.ushop\.com$   [I]
RewriteRule ^/(.*)$ /property/$1 [I]

Does this give you what you're after?

I've changed the [R] to an [I] as the R isn't supported by a rewrite. The I is to ignore case.

Let me know how you get on.

Coordinator
Nov 3, 2009 at 9:54 AM

I think there is some confusion.  You cannot specify the scheme (http://) in the replacement pattern (the second string) on a RewriteRule.  This is nonsense:

RewriteRule ^/(.*)$ http://ushop.com/property/$1 [R]

Shonk is suggesting the same thing, with all his examples, although I did not see him specifically mention that.  All his examples do not include the http:// scheme on the replacement pattern.  This is correct.

What do you mean, exactly, by this:

The rewritecond works and capture the http://property.ushop.com/qs  and is redirected to http://ushop.com/property/qs  but what i want is to rewrite to http://ushop.com/property/qs  which is not working.

What exactly is not working? Did  you check the IIRF log file?  You may be getting an error and the RewriteRule is being completely ignored.  You can also quick-check that with /iirfstatus .

 

Coordinator
Nov 3, 2009 at 9:58 AM

Oh, last thing.  You said you want to rewrite from http://property.ushop.com/qs to http://ushop.com/property/qs.

But, you cannot change the server name with a rewrite, by definition. You can rewrite from http://property.ushop.com/qs to /property/qs, but the request stays on the same server.

Nov 3, 2009 at 10:02 AM
Cheeso wrote:

Oh, last thing.  You said you want to rewrite from http://property.ushop.com/qs to http://ushop.com/property/qs.

But, you cannot change the server name with a rewrite, by definition. You can rewrite from http://property.ushop.com/qs to /property/qs, but the request stays on the same server.

You know, I missed that one.

You're still the master Cheeso. =)

Nov 3, 2009 at 10:18 AM

Oh ic, I thought I can rewrite to another server name like what apache rewrite can do i think.

There's no other way to rewrite from http://property.ushop.com/qs to http://ushop.com/property/qs. at all? What about using HTTP_HOST or some other combination?

Appreciate your help.

Coordinator
Nov 3, 2009 at 10:57 AM

What you are describing is a REDIRECT.

You can do it with IIRF, but you need to use the RedirectRule. 

It seems you want a redirect, but somehow, you also DON'T want a redirect.  I don't understand.  Care to explain that?

 

Nov 3, 2009 at 11:21 AM

Hi. I guess my earlier request was confusing somewhat. Anyway what I hope to achieve is that to forward all requests to  http://property.ushop.com/*.* to http://ushop.com/property/*.* WITHOUT rewriting the url in the browser. In other words, I want it to look as though the user is browsing http://property.ushop.com/*.* but actually all the content is actually coming from ushop.com/property/*.* . The url is masked.

Nov 3, 2009 at 11:23 AM

Is this not just something that you can set up in IIS? Depending on how you've got it set up, just pick up on the subdomain header...

Just a thought.

Coordinator
Nov 4, 2009 at 8:53 AM

netfocus - that sounds like a Rewrite.  Unless you are saying that the ushop.com server is a different physical server than property.ushop.com.  In that case you want a reverse proxy.  Check out the doc for ProxyPass.

Also You may wanna read up on Rewrite -vs- Redirect -vs- Proxy, so you clearly understand what these things are and how they work, and when you should use one versus the other.

 

Nov 4, 2009 at 11:01 AM

Yes it works like proxy. Helicontech isapi_rewrite3 provides a directive called rewriteproxy.

I tried to use your proxypass and it finally worked fine. Superb!

Thanks a zillion