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

如何通过.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:

  1. Incorrect regex matching: Your rule ^(.*)cms.php captures any request path that includes cms.php (like abc/cms.php), then appends that captured content to the target path—resulting in broken URLs like module/cms/v1/pages/v1.01/cms.phpabc.
  2. 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 RewriteMap or custom scripts.

内容的提问来源于stack exchange,提问作者Stack Overflow

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:10:55