Forum Moderators: phranque

Message Too Old, No Replies

redirect to subdomain

         

mrgubu

4:38 pm on Oct 15, 2009 (gmt 0)

10+ Year Member



Hello everybody. I am trying to redirect:

subdomain.domain.com/whatever.html -> domain.com/whatever.html?lang=subdomain

but I can't get it . I am trying something very basic like this:

RewriteCond %{HTTP_HOST} ^subdomain.example\.com.*$
RewriteRule ^(.*)$ http://www.example.com/$1?lang=subdomain [L,R=301]

but this always goes wrong.
I want to note that subdomain.domain.com exists in the DNS records

[edited by: jdMorgan at 4:53 pm (utc) on Oct. 15, 2009]
[edit reason] example.com [/edit]

jdMorgan

4:54 pm on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> but this always goes wrong.

Please be specific.

Where did you put this code?
Are there any other working RewriteRules in this file?
How did you test (What URLs did you try)?
What did you expect to happen?
What did happen?
How did the actual results differ from your expectations?

I'd guess that the problem is that your rule loops, but can't be sure unless you say so.

Jim

mrgubu

5:37 pm on Oct 15, 2009 (gmt 0)

10+ Year Member



ok thanks,

the only other RewriteRules are:

RewriteCond %{HTTP_HOST} ^domain\.com.*$ [NC]
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

The other RewriteRules (that goes wrong) is:

RewriteCond %{HTTP_HOST} ^subdomain.domain\.com.*$
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

What happens is that if I type: [subdomain.domain.com...] then it goes to the real subdomain, I mean to subdomain.domain.com/index.html. What I want is that [subdomain.domain.com...] goes to [domain.com...]

Thank you

g1smd

7:08 pm on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you sure you want a redirect?

You're redirecting from a standard URL to a parameter-based URL.

Which URL do you want users to see?

I'm guessing you might actually need a rewrite.

jdMorgan

8:01 pm on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> It goes to the 'real' subdomain...

That would seem to imply that this code is not located in the correct place. It must be located in the filespace assigned to the subdomain. Further, in order to use an internal rewrite as correctly suggested, it will be necessary to have access from the subdomain's filespace to the main domain's filespace. If the subdomain was 'created' using a control panel "add-on domain' function, then thi will not usually be possible.

Jim

mrgubu

8:42 pm on Oct 15, 2009 (gmt 0)

10+ Year Member



ok, thank you, I undertstood what the code must be in the filespace assigned to the subdomain, which is a "real" subdomain.

I mean that with this code in the .htaccess of subdomain.domain.com:

RewriteCond %{HTTP_HOST} ^subdomain.example\.com.*$
RewriteRule ^(.*)$ http://www.example.com/$1?lang=subdomain [L,R=301]

when I point to subdomain.domain.com, then it goes to domain.com?lang=subdomain, and this last one is the URL that the browser shows.

But now I want to know how to redirect anything like:

subdomain.domain.com/page.html --> domain.com/page.html?lang=subdomain

and I want that the URL that users see is the first one.

I have tried some ways with the .htaccess file of subdomain.com but I only get infinite loops.

Thanks.

jdMorgan

4:22 pm on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot do this because in order to 'keep' the original URL, you must do an internal rewrite, not an external redirect. But in order to do an internal rewrite, the subdomain needs direct access to the main domain's files. (Note that I said files, not URLs... Not at all the same thing.)

An alternative approach is to set up a reverse proxy, but that may be impossible on your server due to hosting restrictions, or at least it will be quite a bit more complex and less efficient than just doing an internal rewrite. You'll need server configuration access, and the ability to modify HTTP headers sent to the back-end (main domain), as well as a custom-logging configuration on that back-end to properly log requests proxied through the subdomain. Otherwise, you logs will be a mess and your "stats" will be near useless.

You'd actually be better off point both the main domain and the subdomain to the same DocumentRoot, if this is possible with your host's control panel.

This kind of set up is trivially easy if you have a dedicated IP address -- that is, if you are on an IP-based server, rather than a name-based server. But it's either very difficult or impossible on inexpensive name-based shared virtual hosting... It's another case of "you get what you pay for."

Jim

mrgubu

4:58 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



Thank you very much Jim for your kind answer.

I understand that the best way to achieve what I want is to point the main domain and the subdomain to the same DocumentRoot

I have a VPS Managed hosting, and I think it is IP-based. So I would appreciate if you can tell me what action I should do in order to point both the domain and subdomains to the same document root żis it a matter of Dns records?

Thnaks again

jdMorgan

5:05 pm on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your VPS is managed, I'd suggest you call your host and ask them. There are too many differences between hosting set-ups for me to 'just tell you how' and the result would be a thread that was not a "discussion" in line with our Forum Charter.

However, it sounds like you may have the 'class' of hosting needed to do this easily.

Jim

mrgubu

5:57 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



ok Jim thanks, I understand,
anyway so this code would need as start point?:

RewriteCond %{HTTP_HOST} ^subdomain.example\.com.*$
RewriteRule ^(.*)$ http://www.example.com/$1?lang=subdomain [L,R=301]

jdMorgan

11:06 pm on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is an external redirect, and will tell the client to change its address bar and ask for the resource again using that new URL. So that's not what you want.

You want to take a request for the URL "subdomain.example.com/<anything>" and internally 'point' that to the file at /subdomain-files/<anything> with a query string of "lang=<language-subdomain>". This is a URL-to-filepath translation -- an internal rewrite.


RewriteCond $1 !^subdomain-files/
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com\.?(:[0-9]+)?$
RewriteRule ^(.*)$ /subdomain-files/$1?lang=%1 [L]

This is why the subdomain requests must have access to the main domain's filespace... There is no more "domain" involved because this happens entirely in the server filesystem.

The additional RewriteCond is to prevent the rule from being invoked more than once per HTTP request.

Jim

g1smd

10:55 am on Oct 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To be clear, if your rule includes a domain name as the target and/or it contains [R] (or R=301 or R=302) you have coded a redirect which forces the browser to ask for a NEW URL.

Omitting the domain name and the [R] flag changes the rule from an external redirect to an internal rewrite, and that is what you need to do here.

Be very clear as to the difference between a redirect and a rewrite. They are two VERY different things with very similar syntax.