如何通过.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 fromcooking/_upload/images/instead. The[L]flag tells Apache this is the final rule to process for this request.
Key notes to keep in mind:
- Referer reliability: This relies on browsers sending the correct
Refererheader, which is standard for image loads from web pages. If someone directly typeshttp://t.test.com/_upload/images/image.pnginto 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. - Directory setup: Make sure the
cooking/_upload/images/directory exists on your server and contains all the images your substation needs. - 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
相关产品推荐
相关产品推荐

