【问题标题】:htaccess redirect showing 404 not found error for jpg filehtaccess 重定向显示 404 未找到 jpg 文件的错误
【发布时间】:2021-05-02 15:55:10
【问题描述】:

我有一些像这样的简单网址

/sub/image.jpg
/sub/sub/sub/image.jpg

我想将它们重定向到另一个页面,但是当我使用重定向代码时,此页面出现 404 错误。

Redirect 301 /sub/1.jpg https://examplel.com/new-page

仅供参考:这些图像现在不在我的服务器上。

【问题讨论】:

    标签: regex apache .htaccess redirect


    【解决方案1】:

    您可以使用此通用重定向:

    RedirectMatch 404 ^/.*sub.+\.jpg$
    

    【讨论】:

    • 我需要重定向特定页面,而不是 404 页面。
    • @Mi2 只会将特定页面重定向到404
    【解决方案2】:

    您也可以尝试匹配任何以/sub/ 开头且不是文件或目录的 URI 的规则:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^sub/ https://examplel.com/new-page [L,NC,R=302]
    

    一旦您确认它工作正常,请将R=302 替换为R=301。在测试mod_rewrite 规则时避免使用R=301(永久重定向)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2016-05-17
      • 2021-12-21
      相关资源
      最近更新 更多