문제

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.

도움이 되었습니까?

해결책 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

다른 팁

use a RewriteCond

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !^old/
RewriteRule ^(.*) new/$1 [L,R=302]    
RewriteRule ^old/(.*) /$1 [L]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top