4

It is many topics here about subdomains but no one can help me...

I use htacces to set subdomain to folder

So if we put http://en.example.com/something

I use something like this..

RewriteCond %{HTTP_HOST}  ^([^.]+)\.example\.com$ 
RewriteRule ^(.*)$        http://example.com/%1/$1 [NC]

This works fine but adress in bar is changed to http://example.com/en/something but I want keep http://en.example.com/something

so I tried this

RewriteCond %{HTTP_HOST}  ^([^.]+)\.example\.com$ 
RewriteRule  ^([^.]+)\.example\.com(.*)   /$1/$2

or just

RewriteCond %{HTTP_HOST}  ^([^.]+)\.example\.com$ 
RewriteRule ^(.*)$        %1/$1 [NC]

but this doesn't work. Any solution or ideas ?

One solution is use language there (http://example.com/en/something) where I rewrite it but after If I work on subdirectories I get something like http://example.com/subdirectory/en/something - terrible. Maybe I like http://example.com/en/subdirectory/something bud how proceed this...

And also on some private servers first one case send me to "maybe" default domain, so it is not working for me. (maybe this is some server condition or settings)

1 Answers1

5

I know this is a month late, but maybe this will still be useful for somebody. A few things here:

Regarding your first RewriteRule:

RewriteCond %{HTTP_HOST}  ^([^.]+)\.example\.com$ 
RewriteRule ^(.*)$        http://example.com/%1/$1 [NC]

As it seems you already discovered, rewriting to another URL will also redirect the user's browser to that new URL, even if it's at your own domain. To keep it hidden, you have to rewrite to a file path on the server (like you do with your next two rules).

Regarding your second RewriteRule:

RewriteCond %{HTTP_HOST}  ^([^.]+)\.example\.com$ 
RewriteRule  ^([^.]+)\.example\.com(.*)   /$1/$2

The problem there is that you can't match the domain name in the RewriteRule, only the URL path. If your URL is www.example.com/something/somethingelse, the string you're trying to match is just something/somethingelse. In other words, it excludes www.example.com/, so this RewriteRule pattern you have will never match the domain name because the pattern isn't even being tested against that part of the URL, but you included the domain name in the pattern, causing the match to fail.

Regarding your third RewriteRule:

RewriteCond %{HTTP_HOST}  ^([^.]+)\.example\.com$ 
RewriteRule ^(.*)$        %1/$1 [NC]

This looks like it should work, so I can't say for sure why it isn't without knowing more about how your files are organized on the server and so forth. Let's say you have all of the website's files in /home/somebody/public_html/. In order for the RewriteRule to work as it is right now, you would need to have an en subdirectory in public_html. So, if somebody went to en.example.com/something, the RewriteRule would cause Apache to serve the file at /home/somebody/public_html/en/something. My guess why it's not working for you is that you might have the subdomain pointing somewhere other than public_html (assuming you actually had the website files organized like in my example). Remember that what you're rewriting to (/$1/$2 in this case) is a file path on the server, not a URL to your website.

I hope that helps! You may have already solved this by now, but even if you have, I'm hoping other people will still find this useful.

Compeek
  • 909
  • 5
  • 13