简体   繁体   中英

Subdomain redirect with htaccess without changing URL in the address bar

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM