Article directory
Want to manage in batchesHestiaCP.htaccess file for subdomains? This tutorial teaches you the simplest method to automatically add different rules to improve website management efficiency. It is a must-learn for webmasters! 🚀
Website redirection isSEOIt is an important means of optimization and traffic management. However, manually configuring each subdomain.htaccessThe file is undoubtedly a huge project. So, is there a way to configure it in batches at one time? Of course there is!
Now, let's talk about how to batch-add subdomains in the HestiaCP panel.public_htmlGenerate directories separately.htaccessfile and write different redirection rules.

Why generate .htaccess files in batches?
If you have a large number of subdomains and want each subdomain to redirect to a specific directory or URL, manually configure each.htaccessThe file is clearly unrealistic.
For example, your primary domain name is chenweiliang.com, your subdomain is af.chenweiliang.com,ar.chenweiliang.comWait, you want to visit af.chenweiliang.com of users jump to www.chenweiliang.com/af/, while visiting ar.chenweiliang.com of users jump to www.chenweiliang.com/ar/.
Well, we can use a Bash script to complete this task in one go.
Steps to generate .htaccess files in batches with HestiaCP
1. Preparation
Before you begin, you need to confirm the following information:
- HestiaCP Username(For example
youruser) - Primary domain name(For example
chenweiliang.com) - Subdomain directory structure(All subdomains
public_htmlThe directory path is like/home/youruser/web/子域名.chenweiliang.com/public_html/) - The server supports Bash script execution
2. Write a script
Create a file called create_htaccess.sh and paste the following script content:
#!/bin/bash
# 设置HestiaCP用户名和主域名
USER="youruser"
DOMAIN="chenweiliang.com"
# 子域名前缀列表
SUBDOMAINS="af ar bg de en es fr it ja ko ru th vi zh-TW"
# 遍历子域名前缀
for SUB in $SUBDOMAINS
do
FULL_DOMAIN="$SUB.$DOMAIN"
HTACCESS_PATH="/home/$USER/web/$FULL_DOMAIN/public_html/.htaccess"
echo "正在为 $FULL_DOMAIN 生成 .htaccess 文件..."
# 写入 .htaccess 文件内容
echo "RewriteEngine on" > "$HTACCESS_PATH"
echo "RewriteCond %{HTTP_HOST} ^$FULL_DOMAIN$ [NC]" >> "$HTACCESS_PATH"
echo "RewriteRule ^(.*)$ http://www.$DOMAIN/$SUB/$1 [L,R=301]" >> "$HTACCESS_PATH"
echo "$FULL_DOMAIN 的 .htaccess 文件已创建!"
done
echo "所有子域名的 .htaccess 文件均已生成!"
3. Grant execution permissions and run the script
Execute the following command in the terminal:
chmod +x create_htaccess.sh
./create_htaccess.sh
After the run is complete, you will see the corresponding .htaccess The files have been generated.
Script parsing
1. Variable definition
USER: Username for HestiaCPDOMAIN: Primary domain nameSUBDOMAINS: Subdomain prefix list
2. Traverse subdomains and create .htaccess
for SUB in $SUBDOMAINS: Loop through subdomain prefixesFULL_DOMAIN="$SUB.$DOMAIN": Concatenate the full subdomainHTACCESS_PATH="/home/$USER/web/$FULL_DOMAIN/public_html/.htaccess":definition.htaccessFile storage pathecho "RewriteEngine on" > "$HTACCESS_PATH": EnableRewriteEngineecho "RewriteCond %{HTTP_HOST} ^$FULL_DOMAIN$ [NC]" >> "$HTACCESS_PATH": Match subdomainsecho "RewriteRule ^(.*)$ http://www.$DOMAIN/$SUB/$1 [L,R=301]" >> "$HTACCESS_PATH": Perform a 301 redirect
3. Script execution results
After successful operation, each subdomain public_html A directory will be generated .htaccess The file content is as follows (with af.chenweiliang.com For example):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^af.chenweiliang.com$ [NC]
RewriteRule ^(.*)$ http://www.chenweiliang.com/af/$1 [L,R=301]
Final Thoughts
This article describes how to use Bash scripts to batch configure subdomains in HestiaCP .htaccess file to achieve automatic redirection.
Through the script, we can:
- Avoid the tedious manual operation, improve efficiency
- Reduce human error, ensuring rule consistency
- Quickly adapt to multiple subdomains, easily complete batch configuration
This not only makes your website management easier, but also improves SEO optimization effects and makes user access smoother.
If you also have multiple subdomains that need to be configured, you might as well try this method, free your hands and get twice the result with half the effort!
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "HestiaCP bulk management of subdomains: the best way to automatically add .htaccess files!", which may be helpful to you.
Welcome to share the link of this article:https://www.chenweiliang.com/cwl-32615.html
To unlock more hidden tricks🔑, welcome to join our Telegram channel!
If you like it, please share and like it! Your sharing and likes are our continuous motivation!