Search code examples
.htaccesshttp-redirectpathrule

Htaccess redirect always keeps the old path


I would like to make a redirect using htaccess rules to point ALL links from

mywebsite.com/subfolder1/subfolder2/...whatever-path....

to

mywebsite.com/subfolder1

But the problem is that whatever rule I use it always keeps the old path in the destination link, for example:

mywebsite.com/subfolder1/subfolder2/mypage.php

becomes

mywebsite.com/subfolder1/mypage.php

I tried all the following possibilities:

RewriteRule (.*) https://mywebsite.com/subfolder1/$1 [R=301,L]

RewriteRule ^subfolder1/subfolder2/(.*)$ /subfolder1/$1 [R=301,NC,L]

RewriteRule ^/subfolder1/subfolder2/(.*)$ /subfolder1/$1 [R=301,NC,L]

Redirect 301 /subfolder1/subfolder2 https://mywebsite.com/subfolder1

RedirectMatch 301 /subfolder1/subfolder2(.*)$ /subfolder1/$1

RedirectMatch 301 ^/subfolder1/subfolder2/?$ https://mywebsite.com/subfolder1

I have placed the "Options +FollowSymLinks" and "RewriteEngine on" rules before them, but NONE of them worked.

Can someone tell what's wrong and how to find the right rule?

Thank you.


Solution

  • Try this

    RewriteRule ^subfolder1/subfolder2/(.*)$ /subfolder1 [R=301,NC,L]
    

    With $1you attach the content of (.*) to the redirected URL. Remove them from code and

    http://mywebsite.com/subfolder1/subfolder2/whatever-path will be http://mywebsite.com/subfolder1