2

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:

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] 

1 Answer 1

2

Use .+ in place of .* to make sure there are 1 more characters after /sub/:

RewriteRule ^sub/(.+)$ http://sub.example.com/$1 [R=302,L] 
Sign up to request clarification or add additional context in comments.

6 Comments

this is working great appreciate it. 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
Oh I missed your previous comment. Difficult to read that long comment. Can you edit the question and explain this new requirement clearly so that I can edit my answer accordingly.
Edited question and answered my 2nd question. Can you confirm this is correct / is there a better implementation? Thanks!
With the slight change RewriteCond %{REQUEST_URI} !^/sub/thispageshouldnotredirect(/|$) is the correct condition which will ignore everything starting with /sub/thispageshouldnotredirect from this redirect.
Appreciate it. Do you have any links to resources where I can learn more about this kind of stuff? Thanks again for all your help!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.