skip to Main Content

I have 2 domains, and need to redirect all subfolders from domain A to the same subfolder in domain B.

Domain A = WordPress
Domain B = Prestashop 1.7

Example of what I need to accomplish:

https://domainA.com/subfolder1 -> 301 -> https://domainB.com/subfolder1

There’s a lot of subfolders in domain A, so I need my rule to be general

Can you point me in the right direction?

3

Answers


  1. You have to edit htaccess file of wordpress domain website.

    Add htaccesss code like below.

    RedirectMatch 301 ^/subfolder1/(.*)$ https://domainB.com/subfolder1/$1
    

    It will be redirect https://domainA.com/subfolder1 to https://domainB.com/subfolder1

    You must have to try. I hope this can be helpful. thanks

    Login or Signup to reply.
  2. I understand the question such that you actually want to redirect all content from one to another domain. That could be done like that:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www.)?domainA.com$
    RewriteRule ^ https://domainB.com%{REQUEST_URI} [QSA,R=301,END]
    

    It makes sense to start out with a R=302 temporary redirection and only change that to a R=301 permanent redirection once you have sorted out everything to your satisfaction. That prevents caching issues.

    Such rule can be implemented in a distributed configuration file (".htaccess") if their consideration is enabled for the host "domainA.com". You should prefer to use the real http server’s host configuration instead though, if you have access to that.


    UPDATE:
    In your comment you confirm that you indeed only want to redirect "subfolders", not all resources. I understand "subfolders" as URLs with a path component that has a "subfolder" followed by a "/" and some other resource after that. Though that does not have to correspond to actual folders in the server side file system at all…

    In that case this variant should work:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www.)?domainA.com$
    RewriteRule ^/?[^/]+/ https://domainB.com%{REQUEST_URI} [QSA,R=301,END]
    

    Obviously the same hints as above apply…

    Login or Signup to reply.
  3. Just an alternative solution….

    it points to two different filesystems.

    In that case, you don’t need the additional condition that checks the requested hostname. (Unless you are hosting other domains on this account?)

    In order to detect whether the requested URL-path contains an initial subfolder you could just check to see whether the URL-path contains a slash (/), rather than explicitly matching the first path segment, since this isn’t used in the substitution string.

    For example:

    RewriteEngine on
    RewriteRule / https://domainB.com%{REQUEST_URI} [R=301,L]
    

    In .htaccess (directory context) the URL-path that is matched by the RewriteRule pattern does not start with a slash, so if a slash occurs anywhere in the URL-path we can assume that there must be a "subfolder". Or, there is path-info, but since this is a WordPress site (and I assume you are using "pretty permalinks", otherwise the existing answer won’t work either) then you don’t have path-info on any legitimate URLs.

    This redirect needs to go near the top of the .htaccess file, before the # BEGIN WordPress section.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search