user309043
user309043

Reputation: 123

Checking if modified URI exists before redirecting

I am in the process of moving a website to a subdirectory because I have remade it.

It is being moved to a directory called old/. I would like to set up a rule such that if a file doesn't exist (i.e. someone has tried to access a file that has been moved to old/) then I check whether it exists in old/ before redirecting them.

This is what I have so far:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/old%{REQUEST_URI} -f #doesnt work
RewriteRule ^(.*)$ /foo/bar/old/$1

My problem is that I am on a shared server and my website is in a subdirectory (foo/bar/ here). So for an example file baz.php my document root is /home/www/htdocs/ and the request_uri is /foo/bar/baz.php.

In my example above am currently checking whether /home/www/htdocs/old/foo/bar/baz.php exists. Which is wrong. I need to check /home/www/htdocs/foo/bar/old/baz.php.

Is there anyway I can do this?

Upvotes: 2

Views: 55

Answers (1)

anubhava
anubhava

Reputation: 785781

You will need to use full path in -f check, use this .htaccess in DocumentRoot/foo/bar/.htaccess:

RewriteEngine On
RewriteBase /foo/bar/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/foo/bar/old/$1 -f
RewriteRule ^(.*)$ old/$1 [L]

Upvotes: 1

Related Questions