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

如何隐藏网站URL中的多子目录与文件名?Apache环境求助

Solution for Masking URL to Root Domain in Apache

Hey there! Framesets are indeed an outdated approach these days—modern browsers block them due to security concerns, so let's stick with Apache's mod_rewrite (since you already have some experience with it) to get this done properly. Here are a couple of reliable approaches:

1. Map Root Requests Directly to Your Target File

This setup will make https://example.com load https://example.com/folder_01/file.php behind the scenes, while keeping the root URL visible in the browser.

Add these rules to your .htaccess file in the root directory:

# Enable mod_rewrite
RewriteEngine On

# Only rewrite if the request isn't for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite root requests to your target file
RewriteRule ^$ /folder_01/file.php [L]

Notes:

  • The [L] flag tells Apache this is the last rule to process for the current request.
  • If you want all non-existent paths (like https://example.com/any-random-path) to also load your target file while showing the root URL, replace the last rule with:
    RewriteRule ^(.*)$ /folder_01/file.php [L]
    

2. Handle Static Assets Properly

If your file.php loads static resources (CSS, JS, images) from the folder_01 directory, you need to make sure those assets aren't accidentally rewritten. Use this adjusted rule set:

RewriteEngine On

# Skip rewrite rules for actual files and directories (so static assets load correctly)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Rewrite all other requests to your target file
RewriteRule ^ /folder_01/file.php [L]

Critical Prerequisites

Before testing these rules:

  • Ensure mod_rewrite is enabled in your Apache configuration. You can check this by looking for LoadModule rewrite_module modules/mod_rewrite.so in httpd.conf (or apache2.conf on Debian/Ubuntu).
  • Make sure your site's virtual host configuration has AllowOverride All set, so Apache reads the .htaccess file.

Why Framesets Fail

As you noticed, browsers throw security warnings for framesets because they're vulnerable to clickjacking and other attacks. Modern browsers enforce headers like X-Frame-Options which block such setups by default—so mod_rewrite is the secure, modern alternative.

内容的提问来源于stack exchange,提问作者Jebes Bless

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:41:32