mardi 9 mars 2021

Can I use an IF statement in an HTACCESS so that I can 301 redirect only specific domain (for multidomains on single docroot)

I run a server with a cloud/webservice and my users can access their accounts through their subdomains:

  • my main site: www.myservice.com

  • user A: usera.myservice.com

  • user B: userb.myservice.com

Recently I added the option for users to use their own domain. This works great through a virtual server. So now if someone visits www.usera.com, they see "www.usera.com" in the browser, but in fact they use usera.myservice.com ("multidomains on single docroot" I think this is called)

So far so good.

My problem is: User A already has a site and would like to have permanent redirects to have old URLs pointing to the new. But when I implement this in the htaccess that is in the rootfolder, then the rules apply to ALL domains, including myservice.com.

I would like to implement 301 redirect, but only for chosen domains.

I think I am close, but... this doesnt work.

<If "%{HTTP_HOST} =~ /^usera.com\./">
   Redirect 301 /oldpath/product1 https://usera.com/newpath/product1
   Redirect 301 /newsartciles/article1 https://usera.com/read/article1
   //etc
</If>

So: IF the HTTP_HOST contains 'usera.com' (with 'wildcard' in front and in back of 'usera.com'), then (and only then) apply all 376 redirect 301 rules within that IF statement.

Can anyone help me out? How can I achieve this?

Aucun commentaire:

Enregistrer un commentaire