Forum Moderators: phranque

Message Too Old, No Replies

htaccess rewrite for language subdomains

         

tunafish

12:30 pm on Mar 10, 2012 (gmt 0)

10+ Year Member



I need to point subdomains like es.domain.com to /public/www/index.php
The problem is my host does NOT provide me to set a path, I can only set up the subdomains for "local use", which creates the folders in the public directory

My structure is
/public/
/public/de/
/public/es/
/public/it/
/public/www/index.php

My host told me to use .htaccess files inside the sub domain folders.

I tried, for example in /public/es/ something like
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(de|es|it)\.mydomain\.com$
# Create an environment variable to remember the language:
RewriteRule (.*) - [QSA,E=LANGUAGE:%1]
# Now check if the LANGUAGE is empty (= doesn't exist)
RewriteCond %{ENV:LANGUAGE} !^$
# If so, create the default language (=es):
RewriteRule (.*) - [QSA,E=LANGUAGE:es]
# Change the root folder of the spanish language:
RewriteCond %{ENV:LANGUAGE} ^es$
# Change the root folder:
RewriteRule ^/?$ /public/www/index.php
</IfModule>

But I am getting a 404 on this:
The requested URL /public/www/index.php was not found on this server.

In my DNS list I see that
es.domain.com CNAME onlinux-it.setupdns.net
while www.domain.com CNAME domain.com

I tried also assigning
es.domain.com CNAME to domain.com
but that did not change anything.

lucy24

8:31 pm on Mar 10, 2012 (gmt 0)

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



My host told me to use .htaccess files inside the sub domain folders.

It's too late then. Requests have to be intercepted in the main folder. They won't get any further on their own.

If your host doesn't allow direct mapping of subdomains, you'll have to do everything with rewrites. Which means you have to be very very careful because the request only gets one chance to arrive at the right place.

You may also want to start shopping for a new host. There's a difference between a small finite number of subdomains-- what you've got-- and wild-card subdomains-- which is what you're basically coding for. It is in the host's best interest to deal with subdomains themselves, because it leaves less room for user error.

Now then...

If you get rid of the htaccess, do all requests end up at your main index page? I'm asking only about the page content, not what the address bar says. That is:

example.com >> user sees the content of example.com/index.html
www.example.com >> user sees the content of example.com/index.html
www.example.com:80 >> user sees the content of example.com/index.html
es.example.com >> user sees the content of example.com/index.html
hu.example.com >> user sees the content of example.com/index.html
fr.example.com >> user sees the content of example.com/index.html

That should be your starting point. I have deliberately included requests that should not end up anywhere, because you will need to deal with those too.

tunafish

12:24 pm on Mar 11, 2012 (gmt 0)

10+ Year Member



Hi thanks for the reply.
Yeah I am already looking for a new host but I still hope I can resolve this restriction somehow. I am baffled that this host, which is a very respected European group, does not allow sub domain mapping!

If I get rid of the .htaccess files in the sub domain folders /public/es/.htaccess I just get a 403, forbidden because there is no content in the folder.

All the other requests you showed let me see the content of domain.com/index.php

The problem is the sub domains which I can't rewrite to serve the content of /public/www/index.php

I also tried with some rewrite rules using the [P] proxy flag, but I guess mod_proxy is not enabled, probably given the security flaws.

So you are saying I have to handle all the rewrites from the /public/www/.htaccess file?
Any example of how to do this?
I am starting to become desperate. It's been more than 2 weeks I am trying to solve this. My host says they passed my issue to tech department, that's more than a week ago.

lucy24

10:20 pm on Mar 11, 2012 (gmt 0)

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



I don't have access to mod_proxy either, but here it doesn't matter because proxying only kicks in when you're going somewhere else. It's sort of an invisible redirect.

So you are saying I have to handle all the rewrites from the /public/www/.htaccess file?
Any example of how to do this?

I'm still hammering out the concepts ;)

You need to do four different things. Assuming your preferred name form is www.example.com:

#1 Requests for www\.example\.com should be intercepted at once and removed from all further rewriting; they're already correct.

#2 Requests for $example\.com(:[0-9]+)? and www\.example\.com:[0-9]+ should be redirected to www.example.com

#3 Requests for (de|es|it)\.example\.com should be rewritten to public/$1/

#4 Requests for anythingelse\.example\.com should get the browser's "ain't no such domain" ... except that you're doing it yourself, so this won't happen. You're stuck between giving them your generic 404 or, again, redirecting to your vanilla domain name.

I think I have still left out a few permutations. Unfortunately this is not one of those questions that can be answered with "Your question has come up an average of three times a day over the past ten years". It has come up before, though.

:: off to try Forums search for questions involving "subdomain" and "rewrite" ::

Sit tight. It has been done before.

tunafish

6:25 am on Mar 12, 2012 (gmt 0)

10+ Year Member



Hi I am happy there is hope again :-)

I wanted to note that even though my app lives the the /public/www/ folder and is accessible through www.domain.dom I have put a rewriteRule in /public/www/.htaccess that forwards anything to domain.com without the www part


RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]


I actually don't care about www.domain.com but off course would like to forward any requests.
Could or should I move my app from the /public/www folder to the /public folder?
Would that make things easier maybe?

I will search the forums again...
cheers