1

I try to use a simple 301 redirect from domain1.com/folder/ to domain2.com/

but excluding domain1.com/folder/subfolder

I use the following code in .htaccess:

RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1

but it simply redirects all the requests, including the requests to subfolder.

Please, help to fix the line to make it work as described. Thank you!

here is the complete code of .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Maxim507
  • 13
  • 3
  • You probably have a conflict with other directives (or you are seeing a cached redirect). The single directive you've posted would work by itself. If you are sure you are not seeing a cached response then please edit your question to include your complete `.htaccess` file. – MrWhite Apr 21 '22 at 08:38
  • Are you wanting to redirect `domain1.com/folder/` to `domain2.com/` OR `domain1.com/folder/` to `domain2.com/` OR `domain1.com/folder/` to `domain2.com/` (literally as you've stated)? – MrWhite Apr 21 '22 at 10:11
  • @MrWhite Hi, I want to redirect `domain1.com/folder/` to `domain2.com/` excluding `domain1.com/folder/subfolder` which I want to remain accessible on the domain1.com/folder/subfolder address with all it's content. Thank you – Maxim507 Apr 21 '22 at 10:33
  • Then please edit your question to include your complete `.htaccess` file. Like I said in my first comment, the directive you've posted should already work as intended by itself. You most likely have a conflict with other directives in your config file. What is the actual redirect you are currently seeing? What happens when you request `domain1.com/folder/subfolder/foo`? – MrWhite Apr 21 '22 at 10:53
  • @MrWhite I have added the complete htaccess to the question. When I request `domain1.com/folder/subfolder/foo` it drops me to `domain2.com`. If I request `domain1.com/folder/` it drops me to `domain2.com/folder/` – Maxim507 Apr 21 '22 at 11:06

1 Answers1

0

Try it like this using mod_rewrite instead:

(NB: This assumes the .htaccess file is located in the document root.)

# /.htaccess

# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule ^folder/(.*) https://domain2.com/$1 [R=301,L]

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>

It is important that the redirect goes before the rewrite to your front-controller.

You will need to ensure your browser cache is cleared before testing and test with a 302 (temporary) redirect to avoid potential caching issues.


UPDATE:

Yes, /folder has it's own .htaccess (this is the file I am working at all this time). Yes, /folder is where Wordpress is installed.

In that case you would need to change the above redirect to read as follows (it won't do anything otherwise):

# /folder/.htaccess

# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule (.*) https://domain2.com/$1 [R=301,L]

Basically, you need to remove folder/ from the start of the regex that matches the URL-path. The URL-path that the RewriteRule pattern matches against is relative to the directory that contains the .htaccess file.

The addition of the check against the REDIRECT_STATUS env var is to ensure that rewritten requests to the WP front-controller (when subfolder is requested) are not redirected.

You can also "simplify" the WordPress directives that follow (although if these are enclosed in # BEGIN WordPress / # END WordPress comment markers then you should leave the directives as they are since they are maintained by WordPress). For example:

RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

The RewriteBase directive is not required. And neither is the <IfModule> wrapper. (But as I said above, only change this if you are hand-coding the .htaccess and not letting WordPress maintain it.)

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Unfortunately, no luck with this. It does not work as expected. – Maxim507 Apr 21 '22 at 19:02
  • @Maxim507 What happens exactly? `/folder` is presumably a physical directory, but what about `/folder/subfolder`? Are there any other `.htaccess` files along the filesystem path? – MrWhite Apr 21 '22 at 19:20
  • thank you for the attention to the issue! When I add the lines it simply does not redirect anywhere. Yes, the `folder` is a directory on a hosting. `subfolder` is an address generated by a Wordpress plugin. – Maxim507 Apr 22 '22 at 05:38
  • @Maxim507 Do you have another `.htaccess` file at `/folder/.htaccess`? Is `/folder` the subdirectory where WordPress is installed? – MrWhite Apr 22 '22 at 14:57
  • Yes, `/folder` has it's own .htaccess (this is the file I am working at all this time). Yes, `/folder` is where Wordpress is installed. I have a hypothesis I need to check, I will share it once done. I think the redirection is buggy with addresses related to self-made files (e.g.`some-file.php`), that has address like `/folder/some-file` or smth like this. Have not catched this idea yet... – Maxim507 Apr 23 '22 at 18:36
  • @Maxim507 In that case, you need to change the redirect rule! I've updated my answer. Unless otherwise stated, it is generally assumed that the `.htaccess` file is located in the document root of the site (it happens that the other directives would work in either location). – MrWhite Apr 23 '22 at 19:00
  • 1
    Thanks, it works! In my case it also causes conflict with the Cluevo LMS plugin (namely with its internal paths) and I can not use it immediately, unfortunately, but the solution itself works okay. Thank you for the help and deep knowledge shared!!! – Maxim507 Apr 25 '22 at 07:35