Reputation: 1
I require to do 301 redirects via the htaccess file however, I am having difficulty to redirect sub-folder which are two levels down. In case it is important, index.php
resolves to /
Scenario:
www.domain.com/oldfolder1/oldfolder2/
shall be redirect to www.domain.com/newfolder/
Also, the following rule exists as it is required
Redirect 301 /oldfolder1/ http://www.domain.com/newfolder/
The problem I am having is that currently the redirect ends up on www.domain.com/newfolder/oldfolder2/ which is wrong and shall not be. The page must end on www.domain.com/newfolder/
I have tried the following but nothing worked
Method 1:
RedirectMatch 301 /oldfolder1/oldfolder2/(.*) /newfolder/$1
Method 2:
Redirect 301 /oldfolder1/oldfolder2 http://www.domain.com/newfolder/
Method 3:
Redirect 301 /oldfolder1/oldfolder2 http://www.domain.com/newfolder/index.php
Can you please advise.
Thank you!
Upvotes: -1
Views: 3211
Reputation: 24478
Try this instead and put this in your htaccess file. See how that works.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/oldfolder1/oldfolder2/?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/oldfolder1/?$
RewriteRule ^(.*) http://www.domain.com/newfolder/ [R=301,L]
Edit:
Based on your comment.
RewriteEngine On
#if directory exists redirect to equivalent folder.
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^oldfolder1/?(.*)$ http://www.domain.com/newfolder/$1 [R=301,L]
#if it's not a directory then redirect to main folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^oldfolder1/?(.*)$ http://www.domain.com/newfolder/ [R=301,L]
Upvotes: 1