Reputation: 263
I'm trying to use RewriteRule
in .htaccess same as Alias /var/www/html/core/ Alias /var/www/html/core/latest/
.
I made this .htaccess in /var/www/html/core:
RewriteEngine On
#Check if file exist in original path (/var/www/html/core):
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#try new path (/var/www/html/core/latest):
RewriteRule ^(.*)$ latest/$1 [NC,L]
It's works. but if file has not been exist in /var/www/html/core/latest apache show Internal Server Error instead of a normal 404 error. So I have to use some RewriteCond *** -f
before last RewriteRule
in order to ensure file exist in new path.
Problem is I can't change %{REQUEST_FILENAME}
to latest/%{REQUEST_FILENAME}
. for example:
core/img/loading.gif must checked in core/latest/img/loading.gif , not latest/core/img/loading.gif
Is it possible to parse %{REQUEST_FILENAME}
in .htaccess somehow?
Upvotes: 1
Views: 3951
Reputation: 785651
You can tweak your rules like this:
RewriteEngine On
RewriteBase /core/
#Check if file exist in original path (/var/www/html/core):
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#try new path (/var/www/html/core/latest):
RewriteCond %{DOCUMENT_ROOT}/core/latest/$1 -f
RewriteRule ^((?!latest/).*)$ latest/$1 [L,NC]
RewriteCond %{DOCUMENT_ROOT}/core/latest/$1 -f
makes sure that file exists in /core/latest/
folder.
Upvotes: 3