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

如何通过.htaccess将子文件夹设为文档根目录并修正资源路径

Hey there! Let's work through this .htaccess problem to fix your broken image links in the cooking substation, while keeping your root directory resources intact.

Here's the exact solution you need—place this code in your root directory's .htaccess (where your main index.php lives):

RewriteEngine On

# Rewrite image requests from the cooking substation to its local upload folder
RewriteCond %{HTTP_REFERER} ^https?://t\.test\.com/cooking/ [NC]
RewriteRule ^_upload/images/(.*)$ cooking/_upload/images/$1 [L]

Let me break down how this works:

  • RewriteEngine On: Turns on Apache's mod_rewrite module, which is required for URL rewriting.
  • RewriteCond %{HTTP_REFERER} ^https?://t\.test\.com/cooking/ [NC]: This condition checks if the request originates from a page in the /cooking/ subdirectory. The ^https?:// covers both HTTP and HTTPS, and [NC] makes the check case-insensitive.
  • RewriteRule ^_upload/images/(.*)$ cooking/_upload/images/$1 [L]: When the above condition is met, this rule internally rewrites the request path. Any image under /_upload/images/ will be served from cooking/_upload/images/ instead. The [L] flag tells Apache this is the final rule to process for this request.

Key notes to keep in mind:

  1. Referer reliability: This relies on browsers sending the correct Referer header, which is standard for image loads from web pages. If someone directly types http://t.test.com/_upload/images/image.png into the address bar, it will still load the root directory's version—exactly what you want, since we don't want to interfere with root resources.
  2. Directory setup: Make sure the cooking/_upload/images/ directory exists on your server and contains all the images your substation needs.
  3. Testing tip: If you want to verify the redirect is working temporarily, replace [L] with [L,R=302] to get a visible temporary redirect (the browser address bar will update). Once confirmed, switch back to [L] for clean internal rewriting (no address bar changes).

This setup ensures your root /upload/ resources stay accessible, while fixing the broken image links for the cooking substation perfectly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:34:29