Forum Moderators: phranque
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]
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
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
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
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.
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
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
However, it sounds like you may have the 'class' of hosting needed to do this easily.
Jim
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]
The additional RewriteCond is to prevent the rule from being invoked more than once per HTTP request.
Jim
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.