如何将子文件夹设为主域名根目录且访问时不显示子文件夹路径
Hey there! I’ve helped a bunch of folks with exactly this issue—let’s get your www.simple.com pointing straight to Public_html/test1 instead of the root folder. Here are two common, reliable methods depending on your hosting setup:
Method 1: Using .htaccess (Shared Hosting Friendly)
This is the go-to approach if you’re on a shared host where you don’t have access to server configuration files.
- Locate or create the
.htaccessfile in yourPublic_htmldirectory. If it doesn’t exist, just create a new plain text file named.htaccess(make sure the dot at the start is included!). - Add the following rewrite rules to the file:
RewriteEngine On # Match your main domain RewriteCond %{HTTP_HOST} ^www\.simple\.com$ [NC] # Skip requests that are already going to test1 RewriteCond %{REQUEST_URI} !^/test1/ # Forward all other requests to the test1 folder RewriteRule ^(.*)$ /test1/$1 [L] - Save the file and test it out—visit
www.simple.comand it should load content fromtest1now.
Quick Notes for .htaccess:
- If you use HTTPS, make sure this rule works for both HTTP and HTTPS (most hosts redirect HTTP to HTTPS automatically, but if not, you can adjust the rules to cover both).
- Double-check that your host allows mod_rewrite (most do, but if you get errors, reach out to their support).
Method 2: Modifying Virtual Host Configuration (VPS/Dedicated Server)
If you have full access to your server (like a VPS or dedicated server), updating the virtual host config is a more permanent solution.
For Apache:
- Find your virtual host configuration file—it’s usually in
/etc/apache2/sites-available/(Ubuntu/Debian) or/etc/httpd/conf.d/(CentOS/RHEL). Look for a file named something likesimple.com.conf. - Update the
DocumentRootandDirectorydirectives to point to yourtest1folder:<VirtualHost *:80> ServerName www.simple.com # Change this path to your actual Public_html/test1 location DocumentRoot /var/www/html/Public_html/test1 <Directory /var/www/html/Public_html/test1> AllowOverride All Require all granted </Directory> </VirtualHost> - If you use HTTPS, repeat the same change in the
<VirtualHost *:443>block of your config. - Restart Apache to apply the changes:
# Ubuntu/Debian sudo systemctl restart apache2 # CentOS/RHEL sudo systemctl restart httpd
For Nginx:
If you’re using Nginx instead of Apache, update your server block:
server { listen 80; server_name www.simple.com; root /var/www/html/Public_html/test1; # Point to your test1 folder index index.html index.php; # Adjust based on your site's file types location / { try_files $uri $uri/ =404; } }
Then restart Nginx:
sudo systemctl restart nginx
Final Checks
- Permissions: Make sure the
test1folder has permissions set to755and files to644(this ensures the web server can read them). - Cache: Clear your browser cache or use incognito mode to test the change—sometimes old cache can make it seem like the change didn’t work.
If you run into any issues (like 404 errors or permission warnings), feel free to share more details and I’ll help troubleshoot!
内容的提问来源于stack exchange,提问作者Shakib Rahman

