Question

I am having the following problem with Apache 2.2:

I need to redirect all / requests to /new/ (an existing folder), except when the URI is /old/, a virtual folder which contains the / directory itself. For example:

/ directory:

<new>
a.php

Thus when I request for /a.php, it redirects to /new/a.php, but if I request for /old/a.php, it returns the /a.php file.

So far, I got this, which does redirect /a.php to /new/a.php, but always fail for the /old/a.php:

RewriteEngine On
RewriteBase /

RewriteRule ^old/(.*) /$1 [L]
RewriteRule ^(.*) new/$1 [L,R=302]

Any ideas what I got wrong?

I must point out that I do not have access to httpd.conf, just to folders, so I must use the .htaccess file.

Was it helpful?

Solution 2

I finally found the solution:

RewriteCond %{REQUEST_URI} old/?(.*)
RewriteRule ^ /%1 [L,QSA]

# redirect requests from / to /new/
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ new/ [L,R=302,QSA]

# redirect requests from /..... to /new/.....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !old/?  
RewriteCond %{REQUEST_URI} !new/?
RewriteRule ^(.*) new/$1 [L,R=302,QSA]

Unfortunately, this is not the best solution, since requests to /a.php will return the /a.php, instead of /new/a.php, but requests to /b.php will redirect to /new/b.php and requests to /old/a.php will return /a.php

OTHER TIPS

use a RewriteCond

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !^old/
RewriteRule ^(.*) new/$1 [L,R=302]    
RewriteRule ^old/(.*) /$1 [L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top