DarkMaze
DarkMaze

Reputation: 263

change request filename (%{REQUEST_FILENAME}) to new path in .htaccess

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

Answers (1)

anubhava
anubhava

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

Related Questions