如果文件存在的mod_rewrite [英] mod_rewrite if file exists

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

问题描述

我已经有一个正常工作,现在两个重写规则,但一些code已被添加到正常工作。

I already have two rewrite rules that work correctly for now but some more code has to be added to work perfectly.

我主持在mydomain.com和所有subdom.mydomain.com都rewrited到mydomain.com/subs/subdom网站。我CMS要处理的请求,如果不存在可触及的文件,重写是像这样做:

I have a website hosted at mydomain.com and all subdom.mydomain.com are rewrited to mydomain.com/subs/subdom . My CMS has to handle the request if the file being reached does not exist, the rewrite is done like so:

RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ subs/%1/index.php?page=$1 [L]

我的CMS处理解析像往常一样下一部分。问题是如果文件确实存在,我需要不经过我的CMS链接到它,我成功地做到这一点是这样的:

My CMS handles the next part of the parsing as usual. The problem is if a file really exists, I need to link to it without passing through my CMS, I managed to do it like this:

RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ subs/%1/$1 [L]

到目前为止,似乎工作就像一个魅力。现在我被挑剔,我需要有存储在潜艇/默认/默认的文件。如果该文件存在于子域文件夹,我们应该抓住这一个,但如果没有,我们需要从默认的子域文件。如果该文件不存在任何地方,我们应该使用404页,从目前的子域,除非是没有的。

So far it seems to work like a charm. Now I am being picky and I need to have default files that are stored in subs/default/. If the file exists in the subdomain folder, we should grab this one but if not, we need to get the file from the default subdomain. And if the file does not exist anywhere, we should be using the 404 page from the current subdomain unless there is none.

我希望它描述不够好。感谢您的时间!

I hope it describes well enough. Thank you for your time!

推荐答案

现在的问题是,你需要提供绝对的文件系统路径来获得 -f -d 工作。如果你在文档根目录,您可以使用此规则:

The problem is that you need to provide the absolute file system path to get -f and -d working. If you’re in the document root, you can use this rule:

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

但是,如果你在别的地方,这将是很难得到正确的路径preFIX。

But if you’re somewhere else, it’s going to be hard to get the right path prefix.

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

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