Subdomain redirect with htaccess without changing URL in the address bar

独自空忆成欢 提交于 2019-12-10 18:27:29

问题


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:


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! :-)



来源:https://stackoverflow.com/questions/18826921/subdomain-redirect-with-htaccess-without-changing-url-in-the-address-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!