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

.htaccess动态URL重写及子域名目录访问配置求助

Absolutely, your requirements are totally feasible! Let's break down the solution step by step with the right .htaccess configurations—no redirects needed for the dynamic URL rewrite, and proper access restriction for the specified directories.

1. Dynamic URL Rewrite (Silent Internal Mapping)

First, we’ll set up an internal rewrite so http://localhost/example silently points to http://localhost/verload.php?name=example without any visible URL change. Add this code to your root .htaccess file:

RewriteEngine On

# Skip rewriting for actual files/directories (preserves access to assets like CSS, JS, images)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Map the requested path segment to the "name" parameter in verload.php
RewriteRule ^([a-zA-Z0-9_-]+)$ verload.php?name=$1 [L]

Key Details:

  • RewriteEngine On enables Apache’s rewrite module (ensure mod_rewrite is enabled in your Apache setup first!).
  • The two RewriteCond lines prevent rewriting requests for real files or directories, avoiding conflicts with your existing site assets.
  • The RewriteRule uses a regex ([a-zA-Z0-9_-]+) to capture the path segment (like example). Adjust this regex if your "name" values include special characters. The [L] flag tells Apache to stop processing further rules if this one matches.

2. Restrict admin/loader Directories to admin.localhost

Next, we’ll block access to these directories unless the request comes from http://admin.localhost/. Add this right after the rewrite rules above:

# Block access to admin/loader directories unless using admin.localhost
RewriteCond %{HTTP_HOST} !^admin\.localhost$ [NC]
RewriteRule ^(admin|loader)/ - [F,L]

Key Details:

  • RewriteCond %{HTTP_HOST} !^admin\.localhost$ [NC] checks if the requested hostname is not admin.localhost (the [NC] flag makes the check case-insensitive).
  • If the condition is met, the RewriteRule matches any request starting with admin/ or loader/ and returns a 403 Forbidden response ([F] flag), stopping further processing with [L].

Critical Pre-Requisites:

  • Enable Apache’s mod_rewrite module: On Linux systems, run sudo a2enmod rewrite then restart Apache with sudo systemctl restart apache2. On Windows, uncomment the LoadModule rewrite_module line in your Apache config file.
  • Ensure your Apache site configuration has AllowOverride All set for the root directory—this lets .htaccess files take effect.
  • Add an entry to your local hosts file to map admin.localhost to 127.0.0.1:
    127.0.0.1 admin.localhost
    
    (On Windows, this file is at C:\Windows\System32\drivers\etc\hosts; on Linux/macOS, it’s at /etc/hosts.)

That’s all! With these configurations, your dynamic URL will work as a silent internal rewrite, and the admin/loader directories will only be accessible via the admin.localhost subdomain.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:06:57