You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过.htaccess移除域名URL中显示的文件夹名称

Fix Folder Name Showing in URL with .htaccess

Hey there! Let’s get that URL cleaned up so it only shows your domain instead of the folder name. I’ve tackled this exact issue dozens of times, so here’s a step-by-step solution using .htaccess:

First, Confirm These Prerequisites

  • Make sure your Apache server has the mod_rewrite module enabled. Most web hosts have this turned on by default, but if you’re unsure, check your phpinfo page or reach out to your host’s support team.
  • Verify your website files are stored in a subfolder (e.g., public_html/my-site/) and you want your root domain (like yourdomain.com) to point directly to this folder.

The .htaccess Code to Use

Create or edit the .htaccess file in your domain’s root directory (usually public_html or www—not the subfolder with your site files). Paste this code, replacing your-folder with the actual name of your site’s folder:

# Enable URL rewriting
RewriteEngine On
RewriteBase /

# Optional: Redirect non-www to www (adjust if you prefer non-www)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

# Rewrite requests to your subfolder without showing it in the URL
RewriteCond %{REQUEST_URI} !^/your-folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /your-folder/$1 [L]

What Each Part Does

  • RewriteEngine On: Turns on Apache’s URL rewriting functionality—this is mandatory for the rules to work.
  • RewriteBase /: Sets the base path for all rewrite rules to your domain’s root.
  • The non-www to www redirect: This ensures all traffic uses a consistent URL structure (swap the rules if you prefer yourdomain.com over www.yourdomain.com).
  • The three conditions before the final rule:
    • %{REQUEST_URI} !^/your-folder/: Prevents infinite loops by skipping the rule if the request is already targeting the subfolder.
    • %{REQUEST_FILENAME} !-f: Leaves real files (like images, CSS, or JS) untouched—they’ll load directly without being rewritten.
    • %{REQUEST_FILENAME} !-d: Does the same for real directories, so you can still access folders like assets/ directly if needed.
  • RewriteRule ^(.*)$ /your-folder/$1 [L]: Internally forwards all matching requests to your subfolder. Users won’t see the folder name in their browser—they’ll just see your clean domain.

Important Notes

  • Double-check you’ve replaced your-folder with your actual folder name (typos here will break things!).
  • Ensure your .htaccess file has the correct permissions (usually 644) so the server can read it.
  • Why your other site doesn’t have this issue? Chances are its files are stored directly in the domain’s root directory, or it already has similar rewrite rules set up.
  • After making changes, clear your browser cache (301 redirects get cached) to test the clean URL properly.
  • If you run into errors, check your server’s error log for rewrite-related issues or confirm mod_rewrite is enabled.

内容的提问来源于stack exchange,提问作者Sukma Liya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:56:56