如何隐藏网站URL中的多子目录与文件名?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_rewriteis enabled in your Apache configuration. You can check this by looking forLoadModule rewrite_module modules/mod_rewrite.soinhttpd.conf(orapache2.confon Debian/Ubuntu). - Make sure your site's virtual host configuration has
AllowOverride Allset, so Apache reads the.htaccessfile.
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

