banner
阿珏酱

阿珏酱

乘上与平常相反的电车,去看看那未曾见过的风景
twitter
github
facebook
bilibili
zhihu
steam_profiles
youtube

Bind subdomains to subdirectories using .htaccess

Tips: When you see this prompt, it means that the current article has been migrated from the original emlog blog system. The publication time of the article is too long ago, and the formatting and content may not be complete. Please understand.

Binding Subdomains to Subdirectories Using .htaccess

Date: 2018-1-22 Author: 阿珏 Views: 1982 Comments: 3

In general, a virtual host only allows binding one domain to the root directory.

Those who have used emlog should be familiar with its static links. It is a typical use of the .htaccess file to redirect the domain of a webpage. By using .htaccess, you can bind a domain to a subdirectory, provided that your hosting server supports the Apache rewrite function. This is usually enabled by default on Linux servers. It allows you to bind a domain to a subdirectory and have multiple sites on one hosting space.

Below is an example of .htaccess code for binding link.52ecy.cn to the subdirectory "link".

<IfModule mod_rewrite.c>
RewriteEngine On  
RewriteBase /  
# Bind link.52ecy.cn to the subdirectory "link"
RewriteCond %{HTTP_HOST} ^link\.52ecy\.cn$ [NC]  
RewriteCond %{REQUEST_URI} !^/link/  
RewriteRule ^(.*)$ link/$1?Rewrite [L,QSA]  
# You can bind multiple domains by repeating the above three lines of code and changing the domain and directory names
</IfModule>

If you have completed the above steps, your subdomain should be accessible. However, you may notice that accessing the main domain + the bound domain directory in the browser is also possible, but this is not what we want.

Next, we will complete the final step by adding an .htaccess file in each bound directory, such as the "link" directory.

The .htaccess code is as follows:
Below is an example of .htaccess code for binding music.xmgho.com to the subdirectory "music".
This allows you to bind a domain to a subdirectory and have multiple sites on one hosting space. This allows you to bind a domain to a subdirectory and have multiple sites on one hosting space. This allows you to bind a domain to a subdirectory and have multiple sites on one hosting space. This allows you to bind a domain to a subdirectory and have multiple sites on one hosting space.

<IfModule mod_rewrite.c>
RewriteEngine On  
RewriteBase /  
# Only allow access to the bound domain
RewriteCond %{HTTP_HOST} !^link\.52ecy\.cn$ [NC]  
RewriteRule (.*) http://link.52ecy.cn/$1 [L,R=301]  
# Handling of directories with the same name as the bound directory
RewriteCond %{REQUEST_URI} ^\/link\/ [NC]  
RewriteCond %{QUERY_STRING} !^(.*)?Rewrite  
RewriteRule ^(.*)$ /%{REQUEST_URI}/%{REQUEST_URI}/$1?Rewrite [L,QSA]  
</IfModule>

Conclusion:
Since my server already supports binding subdomains to subdirectories, I can't test it myself. However, I had a friend test it on an Alibaba Cloud host, and it worked. However, Alibaba Cloud limits the number of bindings, so if you bind too many, it will redirect to the root directory. Therefore, not all server providers support this method, so you need to test it yourself.
Finally, the most important thing is that your domain needs to be resolved for it to work. [Crying Laughing~]

User Comments:

image 小二郎. 2 years ago (2019-06-12)
Do you want me to handwrite it since copying is not allowed?

image 阿珏 2 years ago (2019-06-13)
@小二郎.:Yes, I want you to handwrite it.

image 阿珏 3 years ago (2018-03-15)
[#aru_1][# 微笑][#wb_doge][#(惊哭)]

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.