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

.htaccess配置求助:重定向父路径到子目录并保留原URL,保留子路径访问

Hey there! Let's get this .htaccess sorted out for you. The issue you're hitting is because the R flag triggers an external redirect, which changes the URL in the browser. What you need is an internal rewrite—this serves the content from your target path while keeping the original URL intact.

Here's the adjusted .htaccess configuration that meets both your requirements:

# Enable mod_rewrite
RewriteEngine On

# Skip the rewrite rule if the request is for a real file or directory
# This ensures /services/service1/2/3 and other actual paths work normally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Internally rewrite /services (with or without trailing slash) to your target page
# No R flag = internal rewrite, so the URL stays as /services
RewriteRule ^services/?$ /services/doc/index.html [L]

Let me break down what each part does:

  • RewriteEngine On: Turns on Apache's rewrite module (make sure mod_rewrite is enabled on your server first!).
  • The two RewriteCond lines: These check if the requested path isn't a real file (!-f) or directory (!-d). This is crucial because it lets users still access actual paths like /services/service1/2/3 without interference.
  • RewriteRule ^services/?$ ...: The pattern ^services/?$ matches exactly /services or /services/ (the ? makes the trailing slash optional). The [L] flag tells Apache to stop processing further rules once this one matches—no need to run anything else after this.

Test this out:

  • Visit myhost.com/services (or myhost.com/services/)—your browser URL will stay the same, but you'll see the content from /services/doc/index.html.
  • Visit myhost.com/services/service1/2/3—it should load the actual service page like normal.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:27:44