0

I try to redirect all requests from a domain to a subdomain, except for /mypath and all stuff below. Examples:

example.com -> sub.example.com
example.com/somestuff -> sub.example.com

but example.com/my-path should not be redirected.

Here's the configuration part responsible for that:

RewriteCond %{HTTP_HOST} ^example.com$
RewriteCond %{REQUEST_URI} !^/my-path/
RewriteRule ^/?(.*)$ "http\:\/\/sub\.example\.com" [R=301,L]

The problem is that I cannot achieve the goal and I have no idea what is wrong here. I have tried to find an example of that to achieve the same goal but no luck.

tymik
  • 398
  • 2
  • 6
  • 16
  • "I cannot achieve the goal" - What is happening exactly? Any redirection, the wrong redirection, errors? – MrWhite Dec 30 '16 at 09:56
  • wrong redirection - the exclude is also redirected, but shouldn't be. – tymik Dec 30 '16 at 12:05
  • And what about @Brandon's solution - does this work for you or not? (Bearing in mind you will need to clear any local caches, since the redirect prior to implementing this _exception_ will have been cached hard by the browser.) – MrWhite Dec 30 '16 at 13:58
  • no, @Brandon's solution didn't solve the problem and it doesn't matter if I try `/my-path/something/else.php` or just `/my-path/`, all gets redirected as if there were no exceptions made. – tymik Dec 30 '16 at 18:07
  • Is `/my-path` a physical directory on the filesystem? If so, do you have any other `.htaccess` files in this subdirectory? Do you have other directives in your `.htaccess` file? – MrWhite Dec 30 '16 at 19:11
  • yes, it's physical folder on fs, no other `.htaccess` files - none I'm aware of, yes - common rules generated by WordPress are in the `.htaccess`. If it will help, I will edit post, adding configurations I have tried - there is one with common `` for WP default rules + my above 3 lines and second, where above lines are in separate, preceding `` directive – tymik Dec 30 '16 at 19:57

1 Answers1

1

Putting a trailing slash will match only when the URI /my-path/ is typed and not /my-path

Use

RewriteCond %{HTTP_HOST} ^example.com$
RewriteCond %{REQUEST_URI} !^/my-path
RewriteRule ^/?(.*)$ "http\:\/\/sub\.example\.com" [R=301,L]

so that it will math both /my-path/ and /my-path