Subdomain redirect with htaccess without changing

2019-07-03 02:03发布

I set up a subdomain on my web host like this:

en.domain.com     pointing to the folder     /en/

But when entering "en.domain.com" in the address bar, the URL changes to

domain.com/en/

And if I navigate further, let's say to folder "aaa", the URL turns into

domain.com/en/aaa/

Is there a way to make the subdomain stay in the address bar, like this?:

en.domain.com/aaa/

1条回答
叛逆
2楼-- · 2019-07-03 02:34

I tried everything and nobody could help me. After much research, I found this and it works for me. So here my own answer, that may help others searching for the same thing.

This will make that the URL showing the subdomain ("en.domain.com") doesn't change in the address bar and even if someone enters "domain.com/en/" it will rewrite the URL to "en.domain.com":

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^en(/(.*))? http://en.domain.com/$2 [QSA,L,R=301]

This will break the paths of your site, causing that styles and images won't show. Therefore you need to put this in your HTML code on every page of your site, according to the location of each page in the structure of your site:

For the page in folder "en":

<head>
<base href="http://domain.com/en/" />
</head>

For the page in folder "aaa":

<head>
<base href="http://domain.com/en/aaa/" />
</head>

For the page in folder "bbb":

<head>
<base href="http://domain.com/en/aaa/bbb/" />
</head>

You are welcome! :-)

查看更多
登录 后发表回答