【问题标题】:mod_rewrite redirect requests to subfoldermod_rewrite 将请求重定向到子文件夹
【发布时间】:2026-01-29 00:40:02
【问题描述】:

我已经在服务器的根目录(运行 Apache 2.4.2 的 WAMP)上对我的网站进行了本地编码,并且我在设置图像和其他资源的源时考虑到了这一点,因此例如名为 example.png 的图像将有“src=/img/example.png”。现在我已将项目移至子文件夹,并且图像不会加载,因为它们的链接已损坏。我不想为 html 上的每个图像设置新的来源,所以我希望有另一种方法可以使用 mod_rewrite 修复链接。

演示: 我想要一个请求

http://localhost/img/logo.gif 

 http://localhost/newdir/img/logo.gif

提前致谢!

【问题讨论】:

    标签: regex apache mod-rewrite


    【解决方案1】:

    应该这样做:

    RewriteEngine on
    RewriteRule !^newdir/ newdir%{REQUEST_URI}
    

    有关您的问题的更多信息,请参阅此帖子:

    https://*.com/a/1612329/971459

    小心取消注释 httpd.conf 文件中的以下行:

    LoadModule rewrite_module modules/mod_rewrite.so
    

    文件结构如下:

    -www
      -newfolder
         -img
            -header.png
      -.htaccess
    

    当我访问时:

    localhost/img/header.png 我看到了图片

    【讨论】:

    • 谢谢 radashk,但它没有用,我收到“内部服务器错误服务器遇到内部错误或配置错误,无法完成您的请求。”
    • 这是 apache 错误日志的内容,由于网站限制,我无法发布全部内容,但我认为这是问题所在 [Sat Aug 18 22:30:30.683889 2012] [核心:错误] [pid 3076:tid 852] [client 127.0.0.1:54889] AH00124:由于可能的配置错误,请求超出了 10 个内部重定向的限制。如有必要,使用“LimitInternalRecursion”增加限制。使用“LogLevel debug”获取回溯。
    • @PMMP: RewriteRule ^/img/(.*)$ /newdir/img/$1 [L]
    • 错误消失但图片仍然无法加载 "localhost/img/logo.gif 404 (Not Found) " .htacess 的内容是 RewriteEngine on RewriteRule ^/img/(.*)$ /新目录/img/$1 [L]
    • 您尝试访问的确切网址是什么?