Rewrite htaccess subdomain

There are many topics about subdomains, but no one can help me ...

I use htacces to install a subdomain in a folder

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

I am using something like this.

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

This works fine, but the address in the bar changes to http://example.com/en/something , but I want to keep http://en.example.com/something

so i tried this

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

or simply

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

but it does not work. Any solution or ideas?

One solution is to use a language (http://example.com/en/something) where I rewrite it, but after working on the subdirectories, I get something like http://example.com/ subdirectory / en / something is awful. Maybe I like to http://example.com/en/subdirectory/something to tell how to do it ...

And also on some private servers, at first one case sends me a “possibly” default domain, so it does not work for me. (perhaps this is some condition or server settings)

+4
source share
1 answer

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

As for your first RewriteRule:

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

As you seem to have already discovered, rewriting to another URL also redirects the user's browser to this new URL, even if it is in your own domain. To keep it hidden, you must rewrite the file path on the server (for example, you follow the following two rules).

Regarding the second RewriteRule:

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

The problem is that you cannot match the domain name in the RewriteRule, only the URL path. If your url is www.example.com/something/somethingelse , the string you are trying to match is just something / somethingelse . In other words, it excludes www.example.com/ , so this RewriteRule template that you have will never match the domain name, because the template is not even tested for this part of the URL, but you include the domain name in the template, as a result, the match failed.

As for your third RewriteRule:

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

It seems like it should work, so I can’t say exactly why it doesn’t know how your files are organized on the server and so on. Let's say you have all the website files in / home / somebody / public_html / . In order for the RewriteRule to work as it is now, you need to have an en subcategory in public_html . So, if someone went to ru.example.com/something , RewriteRule will force Apache to serve the file in / home / somebody / public_html / en / something . My guess why this does not work for you is that you may have a subdomain pointing somewhere other than public_html (if you actually had website files that were organized, as in my example) . Remember that what you rewrite ( /$1/$2 in this case) is the path to the file on the server, not the URL of your website.

I hope this helps! You may have already decided this by now, but even if you have it, I hope other people find it useful anyway.

+5
source

Source: https://habr.com/ru/post/1344938/


All Articles