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

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

问题描述

因此​​,这里就是我想要做的。我有一个简单的框架,我使用mod-rewrite,使它们指向正确的文件来重写这些URL。我有它位于Web文件夹内的一个过程文件夹(Web文件夹是所有实际的网页文件,如图片,CSS等都是)。

这个伟大的工程,但我试图现在要做的是有一个default.php文件来处理的形式。该文件的伟大工程,如果我点的形式,但我希望能够以点的形式,它是正确的文件,如果文件不存在,然后它使用default.php

的形式是由一个类中创建和操作设置去处理/ {ID形式的} .PHP的想法是,任何人都可以使用类创建一个表单,如果他们不需要做任何事情特别,default.php文件处理的形式。

我知道我可以只设置窗体类总是使用default.php,然后在default.php文件,我可以的,如果正确的文件退出检查。如果是这样,然后用该文件,如果它不然后继续处理的形式,但我还是很想知道,如果这样的事情会工作的mod-rewrite:

 重写规则^进程/([^ /] *)/网络/流程/ $ 1
的RewriteCond%{DOCUMENT_ROOT} /网络/工艺/ $ 1!-f
的RewriteCond%{DOCUMENT_ROOT} /网络/工艺/ $ 1!-d
重写规则^进程/([^ /] *)/web/process/default.php [L]
 

解决方案

独此规则应该这样做:

 的RewriteCond%{DOCUMENT_ROOT}网/ $ 0!-f
重写规则^进程/ [^ /] + $网络/工艺/ default.php [L]
 

如果所请求的路径不能被映射到现有的文件,它应该被重写到缺省文件

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).

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

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.

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]

解决方案

This rule alone should do it:

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天全站免登陆