homepage Welcome to WebmasterWorld Guest from 54.83.133.189
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Getting address from address bar after using Apache mod rewrite
URI address php Apache mod_rewrite
kpkammer




msg:4495780
 4:00 pm on Sep 16, 2012 (gmt 0)

I'm trying to get the subdomain that is displayed in the address bar.

I'm using the following
mod_rewrite to redirect people accessing a subdomain to view another subdomain (transparently)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule ^(.*)$ http://test.domain.com/$1?sub=%1 [P]

So when users go to
http://anotherone.domain.com/login.php the URL in the address bar will not change but http://test.domain.com/login.php will be loaded.

However the below code does NOT help:
echo $_SERVER['HTTP_HOST'];
echo $_SERVER['SERVER_NAME'];

They return
test.domain.com and I'm trying to get anotherone.domain.com

Please help! :)

 

phranque




msg:4495849
 5:28 pm on Sep 16, 2012 (gmt 0)

welcome to WebmasterWorld, kpkammer!

you cannot rewrite to another hostname.
when you specify the protocol and hostname in the RewriteRule that implies an external redirect.

you must either rewrite to a resource on the same virtual host or use a proxy server.

kpkammer




msg:4495856
 5:55 pm on Sep 16, 2012 (gmt 0)

Thanks!

I'm not sure exactly what you're saying, because the mod_rewrite is not using an external redirect. It is working as I described it.
When users go to http://anotherone.domain.com/login.php the URL in the address bar will not change but http://test.domain.com/login.php will be loaded.


I just want to be able to find
anotherone.domain.com
tommytx




msg:4495875
 6:52 pm on Sep 16, 2012 (gmt 0)

Can we get some clarification. Is the rewrite working ok but when you try to read the domain it is not providing the results you want..
Do you mean when your use this code in a php file located on the landing domain it is showing the current subdomain and not the domain they came from... what you are trying to do is really confusing.
echo $_SERVER['HTTP_HOST'];
echo $_SERVER['SERVER_NAME'];

IF you could start over and use an actual domain name (even ficticious) we can help you....

For example lets name the original folks say come in to your site on
sam.mydomain.com and they are being redirected correctly to bill.mydomain.com.

But when you read the
echo $_SERVER['HTTP_HOST'];
echo $_SERVER['SERVER_NAME'];
it is reporting to you that it is bill.mydomain.com and you want it to actually report the domain they were originally coming to which was sam.mydomain.com

Using absolute domain names will help up follow you better....

kpkammer




msg:4495893
 7:33 pm on Sep 16, 2012 (gmt 0)

Your example is exactly what I'm trying to do.

I'm trying to do this because I have a web based application that I want to have 1 code base, but unique to each sub domain. I was originally using $_SERVER['SERVER_NAME']; to be the key of my database to pull information for each sub domain.

So in the end I want all the sub domains Sam.mydomain.com fred.mydomain.com tom.mydomain.com to point to the code base bill.mydomain.com and I want to be able to read what the URL says (Sam, etc) so I can configure the code base uniquely each time.

g1smd




msg:4495895
 7:34 pm on Sep 16, 2012 (gmt 0)

What you have here is not a redirect or rewrite, it's a Proxy pass-through.

Request arrives at server A. Server A asks server B for some content over the web. Server B sends it back to Server A over the web. Server A sends it back to whatever made the original request. This is double the work, and unless you take appropriate steps various things in the original request header don't make it through to the server where the content actually resides.

In this case Server A and Server B appear to be the same machine. If you continue with this method, you will need to look at the various
X-FORWARDED-____ headers, and you'll need to configure both servers to detect and/or set these.

Since this is all the same base hostname you should probably be looking to use internal rewrites for this, and then the information you want will be more readily available.

[edited by: g1smd at 7:42 pm (utc) on Sep 16, 2012]

phranque




msg:4495902
 7:41 pm on Sep 16, 2012 (gmt 0)

i missed the [P] flag in your RewriteRule.
do you have mod_proxy enabled?

lucy24




msg:4495934
 8:16 pm on Sep 16, 2012 (gmt 0)

it's a Proxy pass-through

You're trying to confuse us again, aren't you? In Apache-speak, "proxy" and "passthrough" are different things:

proxy [P] Force the substitution URL to be internally sent as a proxy request.

passthrough [PT] Forces the resulting URI to be passed back to the URL mapping engine for processing of other URI-to-filename translators, such as Alias or Redirect.


Both carry an implied [L].

Not all hosts are alike, but quick detour to my list of available mods shows that I've got mod_proxy. Also a long string of mod_proxy_{morestuff}. Also-- woo hoo, never realized I had access to it myself-- mod_security (third party, good for squashing evil robots).


Nice going, moderators. Thread was moved at the precise instant I hit Preview, making me wonder what on earth could have caused it to be locked :)

kpkammer




msg:4495938
 8:20 pm on Sep 16, 2012 (gmt 0)

I'm really new to this, could you explain this a little more (with an example if possible) :)
Since this is all the same base hostname you should probably be looking to use internal rewrites for this, and then the information you want will be more readily available.



I don't know about this?
do you have mod_proxy enabled?



Or if you have a suggestion on how I could do this differently/more easily?
Thanks!

lucy24




msg:4496028
 10:30 pm on Sep 16, 2012 (gmt 0)

Before everyone starts going off in different directions:

Do the domain and the subdomain physically live on the same server?

If it's shared hosting (I assume yes, or you'd know for sure which mods you have), are the domain and subdomain in the same userspace?

If yes to both, then you almost certainly don't need to mess about with proxies at all. A quiet rewrite should do it. In fact a proxy is just a fancier and more energy-intensive variant on the ordinary rewrite. So your final code will probably be easier to understand and maintain than what you've got now :)

kpkammer




msg:4496064
 12:42 am on Sep 17, 2012 (gmt 0)

You're correct, the answer is yes to both.

The reason I was using the proxy
[P] is so the URL doesn't change. When a user goes to sam.domain.com, I want them to see sam.domain.com (not codebase.domain.com).

Is there a way for me to accomplish this without using the proxy?

Thanks again!

kpkammer




msg:4496460
 10:58 pm on Sep 17, 2012 (gmt 0)

I'm just going to write a cookie and use that.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule ^(.*)$ http://codebase.domain.com/$1 [P,CO=subdomain:%1:.domain.com:126144000:/]


Thanks everyone!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved