Search code examples
.htaccesshttp-redirectuser-profile

Redirect 301 via htaccess with wildcard


I hope I am asking this questions correctly, if I am not, please feel free to correct me.

I am trying to redirect 301 via htaccess file for all user profiles within the same site so for example. The first section shows the actual URLs I want to redirect from and to where, but this is only focused on 1 user account.

Redirect 301 /otsn/members/admin/my-orders/ /otsn/members/admin/shop/

I am thinking can I use the percentage symbol to make this redirect universal to all users, maybe I'm wrong, Can I do the following?

Redirect 301 /otsn/members/%/my-orders/ /otsn/members/%/shop/

I created a better user profile shop tab that has more capabilities, so I want to get rid of the old one and in case anyone tries to enter the old version of that profile page tab by entering the url manually, I want them sent to the new version for their profile shop page but I want this to happen with every user profile in the system.

Is this the correct method? or is there a better more efficient way of doing this?

Thank you


Solution

  • You may use this rule as topmost rule in your .htaccess or Apache config:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} ^/([\w-]+)/members/([\w-]+)/my-orders/?$ [NC]
    RewriteRule ^ /%1/members/%2/shop [L,R=301]
    

    %1 is back-reference for the first value we are capturing in RewriteCond i.e. first pattern in (...) and same way %2 will represent second captured value in (...).