Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如果找不到特定目录的文件,则重定向到主页_Laravel_.htaccess_Url Rewriting - Fatal编程技术网

Laravel 如果找不到特定目录的文件,则重定向到主页

Laravel 如果找不到特定目录的文件,则重定向到主页,laravel,.htaccess,url-rewriting,Laravel,.htaccess,Url Rewriting,我被困在.htaccess上 链接: 每当此/assets/images目录中存在abc.png时,请在浏览器中预览图像。但如果我从这个目录中删除abc.png图像,它将重定向404未找到页面,但我想重定向到主页(根url:www.example.com) 我希望这样:当特定的/assets/images文件夹中不存在图像文件时。它应该重定向到主页 我试过了,但没用 RewriteEngine On RewriteBase / #If the file does not exist asset

我被困在
.htaccess

链接:

每当此
/assets/images
目录中存在
abc.png
时,请在浏览器中预览图像。但如果我从这个目录中删除
abc.png
图像,它将重定向404未找到页面,但我想重定向到主页(根url:
www.example.com

我希望这样:当特定的
/assets/images
文件夹中不存在图像文件时。它应该重定向到主页

我试过了,但没用

RewriteEngine On
RewriteBase /

#If the file does not exist assets/images folder it will redirect on homepage
RewriteCond %{REQUEST_URI} !^/assets/images/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

在app/Exceptions/Handler.php文件中

public function render($request, Exception $e)
    {
        if($this->isHttpException($e))
        {
            switch ($e->getStatusCode()) 
                {
                // not found
                case 404:
                return redirect()->guest('home');
                break;

                // internal error
                case '500':
                return redirect()->guest('home');
                break;

                default:
                    return $this->renderHttpException($e);
                break;
            }
        }
        else
        {
                return parent::render($request, $e);
        }
    }
您发布的代码是内部重写,而不是“重定向”。但是您使用的条件排除了
/assets/images
子目录(使用
前缀),因此它适用于除您想要的目录之外的所有其他目录

请在
.htaccess
文件的顶部尝试以下操作:

# If an image does not exist in /assets/images then redirect to homepage
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^assets/images/[\w/-]+\.(png|jpg|gif)$ / [R=302,L]
URL的所有模式匹配都在
RewriteRule
指令中执行-无需使用单独的条件。因此,只测试看起来像图像的请求

不需要进行目录检查,因为我假设您没有类似于图像文件名的目录

这允许在
/assets/images
中有多个子目录。例如,
/assets/images/foo/bar/abc.png
。如果所有图像都直接包含在
/assets/images
中,则可以从字符类
[\w/-]
中删除斜杠

它还假定子目录和文件名只能由字符
a-z
a-z
0-9
(下划线)和
-
(连字符)组成

请注意,这是一个302(临时)重定向


但是,只有在浏览器中直接请求图像时,此重定向才有意义。如果它们嵌入在HTML
img
元素中,那么重定向将被视为一个损坏的图像,除非您的主页实际返回图像

这个渲染功能将为整个项目工作,如果任何链接不工作或找不到,然后重定向到主页根据您的答案。但是,每当用户在浏览器中直接点击url中的图像路径链接时,我想在主页上重定向,如果该特定文件夹中不存在该文件,则应将其重定向到主页。在这种情况下,请尝试此方法。RewriteBase/RewriteCond%{REQUEST_FILENAME}上的RewriteEngine-f RewriteCond%{REQUEST_FILENAME}-d#根据需要向列表中添加其他文件扩展名。RewriteCond%{REQUEST_FILENAME}\(gif | jpg | jpeg | bmp | tiff)$RewriteRule(.*)/relative/location/of/errorimage.jpg这是用于图像类型的文件。它生成500个内部服务器/relative/location/of/errorimage.jpg希望您已将此路径更改为URL?在这种情况下,您可以编写一个帮助程序。检查文件是否存在(单击时)。如果存在,助手将离开,否则将重定向到主路径。或者,在获取图像时,您可以检查文件是否存在,如果存在,则提供文件url,否则提供主url。
# If an image does not exist in /assets/images then redirect to homepage
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^assets/images/[\w/-]+\.(png|jpg|gif)$ / [R=302,L]