Subsubdirectory problem with .htaccess redirect subdomain to subdirectory

I just need this setup for web acess of files. All email is done via MX records to GMail.

In my CNAME setup I have wildcard.mydomain.com redirected to mydomain.com

In my top .htaccess file I have:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} subdomain1.mydomain.com [NC]
RewriteRule ^(.)$ http://mydomain.com/subdomain1/$1 [R=301]
RewriteBase /
RewriteCond %{HTTP_HOST} subdomain2.mydomain.com [NC]
RewriteRule ^(.
)$ http://mydomain.com/subdomain1/$1 [R=301,L]

This works well for pages of the kind
http://subdomain1.mydomain.com/page1
http://subdomain2.mydomain.com/page2
(Note that in this setup I have both subdomain1 and subdomain2 pointed to subdirectory with name subdomain1.)

This redirection to subdirectories fails on some systems (e.g., Ubuntu/apache2, but was OK on FreeBSD/apache2?) for
http://subdomain1.mydomain.com/subdirectory/otherpage
I get a FORBIDDEN error:
You don’t have permission to access /subdirectory/otherpage on this server.

I assume the rules I have in my .htaccess file just are not strong enough?

Thanks.

Lester

Lester,

Okay, you’ve confused me totally!

In post #3, I would have said that subdomain.domain/subdirectory SHOULD have given 404s because the subdomain is already directed to domain/subdirectory.

In post #4, you’re linking to subdomain.domain/private/file which, again, SHOULD fail.

What you need to do is provide a specification for yourself (and us) similar to:

Redirect subdomain1.domain/EVERYTHING to domain/subdirectory/EVERYTHING
AND
Redirect subdomain2.domain/EVERYTHING to domain/subdirectory/EVERYTHING

Then, when you test your mod_rewrite, test according to this pattern, NOT
subdomain1.domain/(subdirectory/EVERYTHING) and expect it to go to domain/subdirectory/EVERYTHING because it’ll be redirected to domain/subdirectory/subdirectory/EVERYTHING and that SHOULD result in a 404.

Of course, if your subdomains are set-up “normally,” they’re already serving domain/subdirectory/{REQUEST_URI} so this exercise should be moot.

Regards,

DK

David:

Thanks very much for your reply.

This still does not work. To be explicit, using your corrections, I now have:

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} subdomain1.mydomain.com [NC]
RewriteRule ^(.)$ http://mydomain.com/subdomain1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} subdomain2.mydomain.com [NC]
RewriteRule ^(.
)$ http://mydomain.com/subdomain1/$1 [R=301,L]

This again works well for pages of the kind
http://subdomain1.mydomain.com/page1
http://subdomain2.mydomain.com/page2
(Note that in this setup I have both subdomain1 and subdomain2 pointed to subdirectory with name subdomain1.)

This redirection to subdirectories fails:
http://subdomain1.mydomain.com/subdirectory/otherpage
and so does this fail:
http://subdomain2.mydomain.com/subdirectory/otherpage
I get a FORBIDDEN error:
You don’t have permission to access /subdirectory/otherpage on this server.

However,
http://mydomain.com/subdomain1/subdirectory/otherpage
works fine.

I notice that when the redirect works, the URL bar resolves into
mydomain.com/subdomain
but when I get the forbidden error it does not “resolve”; i.e., I still see the original address.

Thanks.

Lester

Lester

Lester,

First, WELCOME to SitePoint’s Apache forum!

Next, my apologies. I thought I’d answered this earlier.

Finally, I have no idea why it works on one and not on the other - except that the test URL must be different (because the mod_rewrite needs some help):

  1. RewriteBase is designed to UNDO a mod_alias Redirect. If you’re not using that, you don’t need the RewriteBase directive (one, let alone two!).

  2. You need the Last flag in your first RewriteRule (otherwise, it’s ANDed with the next block which will ALWAYS fail that redirection because of the conflicting RewriteCond statements).

Regards,

DK

It probably is quicker to simply click on the following pages,
where I have mydomain->ingber, subdomain1->louise, subdomain2->creekhouse, page1=page2->louise_ballet.html, subdirectory->private, and otherpage->louise_private.html:

http://louise.ingber.com/louise_ballet.html
http://creekhouse.ingber.com/louise_ballet.html

http://louise.ingber.com/private/louise_private.html
http://creekhouse.ingber.com/private/louise_private.html

http://ingber.com/louise/private/louise_private.html

Lester