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

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

问题描述

我已经有两个现在可以正常工作的重写规则,但必须添加更多代码才能完美工作.

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 都被重写为 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]

到目前为止,它似乎很有魅力.现在我很挑剔,我需要将默认文件存储在 subs/default/中.如果该文件存在于子域文件夹中,我们应该获取该文件,否则,我们需要从默认子域中获取该文件.如果该文件在任何地方都不存在,我们应该使用来自当前子域的 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]

但是如果您在其他地方,将很难获得正确的路径前缀.

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

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

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