如果文件不存在,.htaccess 重定向 [英] .htaccess redirect if file doesn't exist

查看:68
本文介绍了如果文件不存在,.htaccess 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这就是我想要做的.我有一个简单的框架,我正在使用 mod rewrite 来重写 url,以便它们指向正确的文件.我有一个位于 web 文件夹内的进程文件夹(web 文件夹是所有实际的网站文件,如图像、css 等).

So here's what I'm trying to do. I have a simple framework and I am using mod rewrite to rewrite the urls so that they point to the correct files. I have a process folder which sits inside the web folder (the web folder is where all the actual website files like images, css, etc are).

这很好用,但我现在要做的是有一个 default.php 文件来处理表单.如果我将表单指向该文件,则该文件效果很好,但我希望能够将表单指向它的正确文件,如果该文件不存在,则它使用 default.php

This works great but what I am trying to do now is to have a default.php file to process the forms. The file works great if I point the form to it but I want to be able to point the form to it's proper file and if the file is not there then it uses the default.php

表单是由一个类创建的,动作设置为 process/{ID OF THE FORM}.php 这个想法是任何人都可以使用该类创建一个表单,如果他们不需要做任何事情特殊,default.php 文件会处理表单.

The forms are create by a class and the action is set to go to process/{ID OF THE FORM}.php The idea is that anyone can create a form using the class and if they don't need to do anything special, the default.php file will process the form.

我意识到我可以将表单类设置为始终使用 default.php,然后在 default.php 文件上我可以检查是否存在正确的文件.如果是,则使用该文件,如果不是,则继续处理表单,但我仍然很想知道这样的事情是否适用于 mod rewrite:

I realize I can just set the form class to always use the default.php and then on the default.php file I can check if the proper file exits. If it does, then use that file, if it doesn't then keep processing the form but I am still curious to know if something like this would work on mod rewrite:

RewriteRule ^process/([^/]*) /web/process/$1
RewriteCond %{DOCUMENT_ROOT}/web/process/$1 !-f
RewriteCond %{DOCUMENT_ROOT}/web/process/$1 !-d
RewriteRule ^process/([^/]*) /web/process/default.php [L]

推荐答案

仅此一条规则即可:

RewriteCond %{DOCUMENT_ROOT}web/$0 !-f
RewriteRule ^process/[^/]+$ web/process/default.php [L]

如果请求的路径无法映射到现有文件,则应将其重写为默认文件.

If the requested path cannot be mapped to an existing file, it should be rewritten to the default file.

这篇关于如果文件不存在,.htaccess 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆