What do you mean by that!? The RewriteCond must only contain the host, no part of the URL path whatsoever.

First off, your code works! Thanks. I'm just now writing up why it's not working for me. I'll post it soon.

I'm a GUI kind of guy, I really dislike regex, command line, unix, rewrite and stuff like that. I do it only when I must. What I meant was not the syntax of RewriteCond but my logic, I need to redirect only

At a glance, it looks like ScallioXTX's code should work. What's the URL you're typing in the address bar? Is there anything else in your htaccess file?

Great question! ScallioXTX's code does work, the problem is the second .htaccess file.

I have a main domain and a sub-domain (there will be many more)

http://condominiospcc.com/
http://avila.condominiospcc.com/

This is on a shared LAMP server. In the file structure the sub-domain is a directory (folder) inside public_html

/home/user/public_html/avila/

The domain has an htaccess file as does each subdomain, they are mostly identical except for the redirection I'm trying to set up.

Apache looks at all the htaccess files. When a visitor uses the sub-domain, Apache only looks at the htaccess file in the sub-domain and everything works fine. When the visitor does not use the sub-domain Apache first looks in the public_html folder and then in the sub-domain folder. The redirecton stops working when Apache reaches this code in the sub-domain's htaccess file.

That's how I had it and it does not work. I tried putting it in the sub-directory and it does not work either.

If you have rewrite rules in the avila htaccess, then those will override the rewrite rules in the public_html htaccess. Your best bet is to keep all your rules in just one htaccess file -- the public_html htaccess file -- and use conditions for anything that you want to apply to only one particular domain.

If you have rewrite rules in the avila htaccess, then those will override the rewrite rules in the public_html htaccess. Your best bet is to keep all your rules in just one htaccess file -- the public_html htaccess file -- and use conditions for anything that you want to apply to only one particular domain.

If things worked according to plan visitors to the main domain would use the main domain and visitors to the sub-domains would use the sub-domains. I'm creating a condo management site, the main domain belongs to the administrator and each building has it's own sub-domain. This works perfectly well. Each building can have it's own styling, etc., etc., etc.

The issue is that a visitor can get to a building sub-domain through the main domain (domain/subDomain/) and then the server variable has the wrong "HTTP_HOST" and this throws a monkey wrench into my code. I could manage that but I want visitors to come in through the front door which is the reason for the code 301.

If the visitor goes to a sub-domain then the public_html htaccess is not read by Apache which is the reason I need the htaccess files in public_html and in each sub-domain.

I wrote some php code (which seems to have vanished) to do the redirect and if I can't get htaccess to do it I'll have to use it.

If things worked according to plan visitors to the main domain would use the main domain and visitors to the sub-domains would use the sub-domains.

The reason things are getting a bit tricky is because the public root of each subdomain is nested inside the public root of the main domain. So when someone accesses domain.com/avila, that's a real directory they're accessing, with its own htaccess. I see two solutions that would make your site's behavior more simple and predicable.

1) The subdomains don't get to have htaccess files, and all rules must reside in the public_html/.htaccess file.

Or 2) Un-nest the subdomains from the main domain. Change your filesystem directory stucture to something like this:

...if a visitor arrives via sub-domain URL then the public_html htaccess file is not read.

Unfortunately the public_html htaccess will also not be read (-ish) if the user requests the subdomain directory (that is, domain.com/avila). That's why the redirect isn't working. So it looks like you're stuck with either option 1 above, or your PHP solution (or switching to a different host ).

Unfortunately the public_html htaccess will also not be read (-ish) if the user requests the subdomain directory (that is, domain.com/avila). That's why the redirect isn't working.

Wrong! Both htaccess files are read (public_html and avila). When I remove the rewrite stuff from the avila sub-domain and leave on the one in public_html htaccess, the redirect works. It doesn't like rewrite stuff in both htaccess files.

... or switching to a different host

No way! I love my hosting service. I've been with them for just over ten years with hardly a problem. Great uptime and great help-desk which I need to use very seldom. I had two crummy hosts before this one and I'm not risking another crummy one.

Wrong! Both htaccess files are read (public_html and avila). When I remove the rewrite stuff from the avila sub-domain and leave on the one in public_html htaccess, the redirect works. It doesn't like rewrite stuff in both htaccess files.

Yes, I know. I mentioned that behavior in post #10 above. In my last post, I tried to simplify the explanation with an "-ish", but I suppose I'll be more explicit this time.

When either domain.com/avila or avila.domain.com is accessed, then both htaccess files are read. But if you have rewrite rules in both, then the avila rewrites will override the public_html rewrites. That's why it appears as if the public_html htaccess is not read.

Yes, I know. I mentioned that behavior in post #10 above. In my last post, I tried to simplify the explanation with an "-ish", but I suppose I'll be more explicit this time.

When either domain.com/avila or avila.domain.com is accessed, then both htaccess files are read. But if you have rewrite rules in both, then the avila rewrites will override the public_html rewrites. That's why it appears as if the public_html htaccess is not read.

Then surely if you put the redirect in the htaccess in the avila subdirectory it should work, no?

Rewrite rules have a "last" flag, which tells Apache to not apply any more rewrites. BUT, after a rewrite has changed the URI, then Apache will re-process that URI as if it were a brand new request, which means the rewrite phase will execute again. UNLESS the rewrite resulted in an external redirect, in which case it will just send the redirect response.

ALSO, the "last" flag only applies to rewrite rules. Other directives such as DirectoryIndex, Options, Redirect, etc., from both htaccess files will still be processed.

There's a lot of nuance here, and if you want to be certain you've got all the details, there's no better place than the [official documentation. You can read about [URL="https://httpd.apache.org/docs/2.4/howto/htaccess.html#how"]how htaccess directives are applied, and about [URL="https://httpd.apache.org/docs/2.4/rewrite/tech.html#InternalRuleset"]how rewrite rules are processed](https://httpd.apache.org/docs/2.4/).