Reputation: 760
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. I'm not that familiar with the apache rewrite-engine. Thanks in advance for any idea.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{DOCUMENT_ROOT}/$1\.local\.$2 -f
RewriteRule ^(.+)\.([^./]+)$ /$1.local.$2 [L]
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:
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.
Upvotes: 3
Views: 2708
Reputation: 19016
You can put this code in your htaccess (which has to be in your document root folder)
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.
If so, it then checks if its local
version exists.
If so, then it internally rewrites to local
version instead of serving normal
version.
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).
We can't use %{DOCUMENT_ROOT}
anymore in that case
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} ^([^/]+)(.+)\.([^./]+)$
RewriteCond %1%2\.local\.%3 -f
RewriteRule ^ %2.local.%3 [L]
Upvotes: 4