简体   繁体   English

mod_rewrite检查变量中是否存在文件

[英]mod_rewrite check if file exists in variant

is there a way to check if the requested file is available as a local variant? 有没有办法检查所请求的文件是否可用作本地变体?

/project/config.php -> /project/config.local.php

I want to use this to switch between operating and testing system configuration. 我想用它在操作和测试系统配置之间切换。 Filenames matching *.local.* will be ignored in git. 匹配* .local。*的文件名将在git中被忽略。 I'm not that familiar with the apache rewrite-engine. 我对apache重写引擎并不熟悉。 Thanks in advance for any idea. 提前感谢任何想法。

Solution 1 解决方案1

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{DOCUMENT_ROOT}/$1\.local\.$2 -f
RewriteRule ^(.+)\.([^./]+)$ /$1.local.$2 [L]

Solution 2 解决方案2

Solution 1 works fine for the root directory but how should the .htaccess/httpd.conf be adapted to work in a <Directory> and Alias environment like: 解决方案1适用于根目录但是如何调整.htaccess / httpd.conf以在<Directory>Alias环境中工作,如:

Alias /projects "P:/projects/"
<Directory "P:/projects">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>

For me the following rule and conditions seem to work: 对我来说,以下规则和条件似乎有效:

Alias /projects "P:/projects/"
<Directory "P:/projects">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted

    RewriteEngine on
    RewriteBase /projects/
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} ^(.*)/([\w\.]+)\.(\w+)$
    RewriteCond %1/%2\.local\.%3 -f
    RewriteCond %{REQUEST_URI} ^(.*)/([\w\.]+)\.(\w+)$

    RewriteRule (.*) %1/%2.local.%3 [L]
</Directory>

I'm not really sure if this is the most efficient way due to four different conditions. 由于四种不同的条件,我不确定这是否是最有效的方式。 In this case it would be nice to have a REQUEST_URI_BASE var which does contain REQUEST_URI without the filename. 在这种情况下,这将是很好有一个REQUEST_URI_BASE VAR其中确实包含REQUEST_URI没有文件名。

You can put this code in your htaccess (which has to be in your document root folder) 您可以将此代码放入htaccess(必须位于文档根文件夹中)

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{DOCUMENT_ROOT}/$1\.local\.$2 -f
RewriteRule ^(.+)\.([^./]+)$ /$1.local.$2 [L]

This code checks if requested URI is an existing file. 此代码检查请求的URI是否为现有文件。
If so, it then checks if its local version exists. 如果是,则检查其local版本是否存在。
If so, then it internally rewrites to local version instead of serving normal version. 如果是这样,那么它在内部重写为local版本而不是normal版本。


EDIT: in apache config file directly (since it's out of document root, and stored on another drive, it's a bit harder but here's a solution). 编辑:直接在apache配置文件中(因为它不在文档根目录中,并且存储在另一个驱动器上,所以它有点困难,但这是一个解决方案)。

We can't use %{DOCUMENT_ROOT} anymore in that case 在这种情况下,我们不能再使用%{DOCUMENT_ROOT}

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} ^([^/]+)(.+)\.([^./]+)$
RewriteCond %1%2\.local\.%3 -f
RewriteRule ^ %2.local.%3 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM