sub domain rewrite

Topics: Developer Forum
Sep 25, 2009 at 12:32 AM

 

Hi,
We have a domain lets say http://maindomain.com and sub domain http://subdomain.com
Is the following url rewrite possible for following:
for http://subdomain.com points to http://maindomain.com/abc.php
and http://subdomain.com/xyz.php points to http://maindomain.com/xyz.php
We have tried the following rule:
RewriteCond %{HTTP_HOST} ^(.+\.)?subdomain.com [NC]
RewriteRule ^(.*)$ /abc.php [L]
The above rule keeps the sub domain url on browser and browse abc.php but any url appended to the subdomain url browse abc.php.
RewriteCond  %{HTTP_HOST}  ^(.+\.)?subdomain\.com$
RedirectRule ^/(.*)$       http://maindomain.com/$1 [L] 
the rule always points to the main domain whether it is abc.php or other. 
Is it possible what we are doing? We need subdomain to point the particular page(abc.php here) and other url appended to the subdomain points the pages of maindomain. 

Hi,

 

We have a domain lets say http://maindomain.com and sub domain http://subdomain.com

Is the following url rewrite possible for following:

 

for http://subdomain.com points to http://maindomain.com/abc.php

and http://subdomain.com/xyz.php points to http://maindomain.com/xyz.php

 

We have tried the following rule:

 

RewriteCond %{HTTP_HOST} ^(.+\.)?subdomain.com [NC]

RewriteRule ^(.*)$ /abc.php [L]

 

The above rule keeps the sub domain url on browser and browse abc.php but any url appended to the subdomain url browse abc.php.

 

RewriteCond  %{HTTP_HOST}  ^(.+\.)?subdomain\.com$

RedirectRule ^/(.*)$       http://maindomain.com/$1 [L] 

 

the rule always points to the main domain whether it is abc.php or other. 

 

Is it possible what we are doing? We need subdomain to point the particular page(abc.php here) and other url appended to the subdomain points the pages of maindomain. 

 

Coordinator
Sep 25, 2009 at 3:32 AM

Ok, it sounds to me like you want to rewrite a request that has no URL Path.  If that's the case, you might try this:  

RewriteCond %{HTTP_HOST} ^(.+\.)?subdomain.com [NC]
RewriteRule ^/?$    /abc.php [L]

This says, for any URL request arriving for http://foo.subdomain.com, where foo can be anything, rewrite it to abc.php. If there is any URL path, then it is not rewritten. The only requests that are rewritten are those with NO path: "/?" matches a slash or not.  A scheme and hostname only.

If that is not what you want, then I don't understand.   Your description isn't clear.

First, "subdomain.com" is not a sub domain of "maindomain.com".   If I understand properly, they are two completely different domains.  Are the websites for these distinct domains hosted on the same server?   If you really want a subdomain, you should probably use names like www.domain.com and  host1.domain.com.  

I also don't know what you mean by "points to".

It's a common thing - people know exactly what they want, but they don't use the accepted terms to describe what they want.   I can help you better if you use the words I understand.

Term meaning
Rewrite map the request on the server side. The client or browser is not aware of any mapping.
Redirect respond to the client with an HTTP 301 or 302 (etc) directing the client to make another request to a different URL.
Reverse Proxy pass through the content for a web resource located on a different server.
URL Path the part that comes after the first slash in a URL.

It sounds like you know what you want when you use the phrase "points to" but I don't.  Is it a redirect?  You use a different hostname, which would suggest a redirect.   But you did not distinguish between the actions for abc.php and xyz.php, which leads me to think that you want rewrites for both of them.    But then you also tried RedirectRule.  Not sure if you know the difference.

If you don't know the difference between a Redirect and a Rewrite, check the docs, I describe it there.

 

Sep 25, 2009 at 5:41 AM

Hi Cheeso,

 

Thanks a lot for you reply. I have implemented your suggestion. Now it worked. Thanks a lot again. 

 

Sorry for my words those were not technical. Actually I had used "points to" for rewrite. 

 

Thanks,

Aaram

Oct 2, 2009 at 5:44 AM

Support,

I've tried everything but can't quite seem to get the REGEX correct.

Here is what I need:

ANY request coming into http://subdomain.domain.com needs to run the application located in http://www.domain.com/application/ while keeping the browser address bar as http://subdomain.domain.com.

The application in it's default configuration runs from http://www.domain.com/application/filename.extension?queryString

I'd like the rewrite to basically change that so the browser address bar appears like the application is running as http://subdomain.domain.com/filename.extension?queryString (assuming that the server has already reached the /application/ folder as part of the rewrite logic).

http://subdomain.domain.com is registered via DNS to point to the http://www.domain.com IP.

Here is what I have now which is causing the application to do append the /application to the http://subdomain.domain.com URL which breaks the application (the address bar looks like http://subdomain.domain.com/application/filename.extension?queryString instead of http://subdomain.domain.com/filename.extension?queryString) - in essence trying to reach /application/application/ which does not exist. At lease I believe that is what is happening.

RewriteCond  %{HTTP_HOST}  ^subdomain\.domain\.com$  [I]
RewriteRule  ^/(.*)$       /application/$1 [L]

Please help.

Thanks.

Gregory

Coordinator
Oct 2, 2009 at 8:02 AM

None of that made sense.  and who is "Support" ? 

I'm just guessing that maybe you need something like this.  

RewriteCond  %{HTTP_HOST}  ^subdomain\.domain\.com$  [I]
RewriteRule  ^/(?!application)(.*)$       /application/$1 [L]
Oct 2, 2009 at 3:45 PM

Hi  Cheeso,

Sorry about that.

I have an application running at http://www.domain.com/application/filename.aspx?url=parameter.

Instead of the browser address bar reading http://www.domain.com/application/filename.aspx?url=parameter, I'd like it to read http://subdomain.domain.com/filename.aspx?url=parameter.

I'm assuming to do this, I need to do a RewriteCond and RewriteRule to do two things:

1. Look for requests coming into http://subdomain.domain.com
2. Route those requests on the server side ONLY (leaving the browser address bar as is) to http://www.domain.com/application/

In effect, http://subdomain.domain.com becomes the new application root as far as the browser address bar is concerned.

When I tried to do that with the sample code I provided, it worked, PARTIALLY.

The browser address bar would read http://subdomain.domain.com and the server was going to http://www.domain.com/application/, HOWEVER, sometimes the http://subdomain.domain.com was getting appended to http://subdomain.domain.com/application/.

I'm assuming that is happening because I was using an incorrect REGEX.

Does that make sense? I'm doing the best I can to describe what it is we want  to do.

Thanks Cheeso.

Gregory

Coordinator
Oct 2, 2009 at 5:15 PM

Normally what people do is open a new thread for a new problem.  It allows for better searching.

> HOWEVER, sometimes the http://subdomain.domain.com was getting appended to http://subdomain.domain.com/application/.

It sounds to me that you were using a bad regex.

Do you have a log file that shows this behavior?

move this to a new thread, will you?