如何通过.htaccess批量隐藏模块子目录,无需逐个添加规则?
Hey there, let's get this sorted out for you. Your current .htaccess setup has two key issues that are making it ineffective and tedious to maintain:
- Incorrect regex matching: Your rule
^(.*)cms.phpcaptures any request path that includescms.php(likeabc/cms.php), then appends that captured content to the target path—resulting in broken URLs likemodule/cms/v1/pages/v1.01/cms.phpabc. - Manual repetition: You're having to add a new rule for every single file, which is a huge waste of time when you add new modules or files.
Let's look at two practical solutions depending on your server access and file structure:
Solution 1: Batch Match by Module (No Special Server Permissions Needed)
If your module files follow a naming pattern (e.g., all CMS-related files start with cms_ or cms), you can cover an entire module with one rule instead of individual files. This cuts down on manual work drastically:
RewriteEngine On # Skip real files/directories so we don't mess with static assets or existing pages RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # CMS module: Match all PHP files starting with "cms" and map to their subdirectory RewriteRule ^(cms.*\.php)$ module/cms/v1/pages/v1.01/$1 [L] # Blog module: Add a similar line when you add a new module RewriteRule ^(blog.*\.php)$ module/blog/v2/pages/v2.01/$1 [L]
Now, when you add a new module (say, a "shop" module), you just add one line like RewriteRule ^(shop.*\.php)$ module/shop/v1/pages/v1.00/$1 [L]—no more per-file rules.
Solution 2: Auto-Find Files in Any Subdirectory (Requires Server Config Access)
If your files don't follow a naming pattern and you want complete automation (no manual rules at all), you can use RewriteMap with a simple shell script to recursively find files in your module directory. Note: This needs access to edit your server's httpd.conf (not just .htaccess), so it's not an option for all shared hosts.
Step 1: Create a File-Finding Script
Make a script (e.g., /var/www/find_module_file.sh) with this content (replace /var/www/html with your site's root directory):
#!/bin/bash SITE_ROOT="/var/www/html" while read filename; do # Find the first matching file in the module directory find "$SITE_ROOT/module" -name "$filename" -type f | head -n 1 done
Give it execute permissions: chmod +x /var/www/find_module_file.sh
Step 2: Update httpd.conf
Add this line to your server's httpd.conf (or a site-specific config file):
RewriteMap filefinder prg:/var/www/find_module_file.sh
Step 3: Add Rules to .htaccess
RewriteEngine On # Skip real files/directories RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Auto-map the requested filename to its path in the module directory RewriteRule ^(.*)$ ${filefinder:$1} [L]
Now, when you hit example.com/cms.php, the script will automatically hunt down that file in any subdirectory under module and route you to it.
Quick Notes
- For Solution 2, make sure all your filenames are unique—if two files have the same name in different subdirectories, the script will pick the first one it finds, which might not be what you want.
- If you're on a shared host, Solution 1 is your best bet since most hosts don't allow
RewriteMapor custom scripts.
内容的提问来源于stack exchange,提问作者Stack Overflow

