Redirect all sub-directories but not parent

I want to redirect all folders after a subdirectory:

example.com/sub/sub2/some-place
example.com/sub/sub3/some-place
example.com/sub/subY/some-place

to a subdomain:

Read More
sub.example.com/sub2/some-place
sub.example.com/sub3/some-place
sub.example.com/subY/some-place

This is the rule I’m using and it works:

RewriteRule ^sub/(.*)$ http://sub.example.com/$1 [R=302,L]

However, I don’t want the example.com/sub/ directory to be redirected.

User visits example.com/sub/ should stay at /sub/
User visits example.com/sub/ANYTHINGELSE should be redirected to sub.example.com/ANYTHGINELSE

Thanks in advance.

EDIT: Question: How do I add an exclusion to the rule? Example: I want

example.com/sub/sub2/some-place 
example.com/sub/sub3/some-place 
example.com/sub/subY/some-place 

to redirect as above but

example.com/sub/thispageshouldnotredirect 

to NOT redirect. I tried adding this rule:

RewriteCond %{REQUEST_URI} !^/thispageshouldnotredirect 

EDIT #2: This is working for me currently:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/sub/thispageshouldnotredirect/?$
RewriteRule ^sub/(.+)$ http://sub.example.com/$1 [R=302,L]

Related posts

Leave a Reply

1 comment