【问题标题】:.htaccess return 404 when not image file on subdomain.htaccess 在子域上没有图像文件时返回 404
【发布时间】:2015-04-14 22:18:49
【问题描述】:

我有一个 .htaccess 文件,如果子域为 img0.example.infoimg1.example.infoimg2.example.info 等,则该文件应强制仅提供图像文件。我想在 404.html 中提供带有 404 响应代码的描述性标记,但它只是显示 500 Internal Server Error 错误。

这是我的 .htaccess 文件的内容:

<IfModule mod_rewrite.c>

    RewriteEngine on

    # If request is via an image subdomain, throw error 404 for any file types other than images
    RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
    RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|gif|png)$ [NC]
    RewriteRule ^(.*)$ - [R=404,L]

    ErrorDocument 404 /404.html 

</IfModule>

它部分适用于 .jpg、.jpeg、.gif 和 .png 文件都可以正常提供服务,但我无法收到整洁的 404 消息。请帮忙。

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    另一种仅重定向到 404 文件的选项。

    # If request is via an image subdomain, throw error 404 for any file types other than images
    
    RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
    RewriteCond %{REQUEST_URI} !(.+)\.(jpg|jpeg|gif|png)$ [NC]
    RewriteCond %{REQUEST_URI} !^/404.html$ [NC]
    RewriteRule ^(.*)$ /404.html [R=404,L]
    

    【讨论】:

      【解决方案2】:

      您的/404.html 也被此规则捕获。

      试试这个代码:

      ErrorDocument 404 /404.html 
      
      <IfModule mod_rewrite.c>
      
          RewriteEngine on
      
          # If request is via an image subdomain, throw error 404 for any file types other than images
          RewriteCond %{HTTP_HOST} ^img[0-9]+\.example\.info$ [NC]
          RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|gif|png)$ [NC]
          RewriteRule !^404\.html$ - [R=404,L,NC]
      
      </IfModule>
      

      【讨论】:

      • 太棒了!太感谢了。所以澄清一下:我有两个问题。 1) 错误文档应该在 IfModule 之外定义 2) 重写规则必须确保它不会导致无限循环。我说对了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 2015-03-19
      • 1970-01-01
      • 2013-01-20
      • 2015-07-28
      • 2013-06-16
      • 2019-04-25
      相关资源
      最近更新 更多