2

According to this answer, you can exclude a single subfolder from a redirect like so:

RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]

How would I expand that to include two or more separate subfolders?

Menasheh
  • 3,560
  • 3
  • 33
  • 48

3 Answers3

5

You can exclude multiple folders with the following code:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_URI} !^/my-folder/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

This will redirect all requests that don't start with the folder /test/ or /my-folder/ to newdomain.com.

Source: based on broken code from here.

Menasheh
  • 3,560
  • 3
  • 33
  • 48
5

It might be easier to read and maintain if you follow a different answer to the question you referenced.

RewriteEngine on

# Do not rewrite these directories
RewriteRule ^(uploads) - [L]
RewriteRule ^(second) - [L]

# Rewrite all other URL
RewriteRule (.*) http://example.com/$1 [L,R=301]
Jan Papenbrock
  • 1,037
  • 1
  • 11
  • 24
1

If you are aiming to redirect a Wordpress site using these rules, it is essential that "index.php" is included in the list of exclusions, otherwise the exclusions won't work - because Wordpress processes everything through index.php.

Here is an example of what it will look like with Wordpress, if you want to redirect a domain, except you still want admins to be able to login to the previous wp-admin section. And as an extra example sitemap_index.xml is excluded from the redirects too.

RewriteCond %{REQUEST_URI} !^/wp-admin($|/)
RewriteCond %{REQUEST_URI} !^/wp-login.php$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/sitemap_index.xml$
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]
Josh Moore
  • 176
  • 1
  • 5