Subdomain redirect: Works, but not for direct file access


For the longest time, I have been using the below .htaccess code to redirect to a subdomain:

RewriteRule ^foo/(.*)$1 [R=301,L]

I just realized that when I access a file directly, the above rewrite rule has no affect. For example:

I am not sure what to Google for. Most Google search results give me information about how to redirect folder to subdomain, but not direct file access too. :frowning:

Any tips ya’ll could provide would be spectacular!

Many thanks!


Hi Mickey!

Just what would make you want to redirect to a subdomain like that?

Okay, I’m sure that there was a question in there somewhere but I’ll be darned if I can find it!

FWIW, you COULD be redirecting into a loop (it depends upon how your host has setup your subdomains) so that’s not a good thing, is it?

One key issue to what you’re talking around is the location of the document root for your subdomains (typically the subdirectory). If that’s the case, your desired redirection can render your main webspace out of reach (for relative links) because only the subdirectory for the subdomain is available, i.e., all your shared support files (images, css, js, etc) will REQUIRE absolute links (and the js will be in a sandbox because it’s not in the same domain).

If you could let us know what you’re trying to do with these subdomains, it would help immensely.

[aside]You ALWAYS come here with interesting questions![/aside]



Hi DK! Thanks so much for your quick reply… As always, I really appreciate the professional help. :slight_smile: :spf:

Doh! Maybe there isn’t a question in there! Hehe. :smiley:

Based on your reaction, I could just be over-worrying about my sub-domain setup.

Definitely not!

Makes me wonder if my current redirect code is even stable. I will have to read through some of you docs again.

Interesting! That definitely does not sound good.

In this one case, the sub-domain is setup to host files that don’t reference anything outside of their root folder.

Sorry about the vagueness in my original question. :slight_smile:

Well, basically I am trying to make any call to this domain:


To redirect to:

… and/or:

Hrmmm, now I am confusing myself!!! :lol: :eye:

I should mention:

I do have another .htaccess in my “sub-domain-folder” directory, which, among other things, has this bit of code:

# Power-up the rewrite engine:
RewriteEngine on

# Rewrite the base path:
RewriteBase /

I guess I am just looking for the diffinitive/best way to redirect my sub-domains (which I setup via cpanel) to their respective folders… Unfortunately, with my current code (posted in first post), when I access files directly, the sub-domain redirect does not work. :frowning:

Hehe! Thanks DK! You have been so helpful! :slight_smile:

I am off to refresh my mod-rewrite brain cells by reading your tutorials again.

Thanks so much for your help!!!

Have an excellent day!



Aw, at least you didn’t sense my frustration with similar questions lately! Whew!

Okay, to handle a subdomain’s files as a subdomain, you could either add code in the domain’s .htaccess or the subdirectory’s .htaccess. Because the domain should receive more requests than the subdomain, do it in the subdomain, of course.

  1. Ditch the RewriteBase (unless you have some mod_alias Redirects which need UNDONE before letting mod_rewrite loose).

In your subdirectory’s .htaccess:

  1. Test the {HTTP_HOST} for subdomain. If not a match (with the subdomain), redirect to to the path/file without the subdirectory with R=301 and Last flags (I’ll leave that simple coding to you).

The ONLY problem I can foresee is IF you are hotlink protecting the subdomain’s images, they won’t be available to the domain because it IS another domain to Apache (and has no business stealing images from your subdomain).

That should about wrap it up - unless you have any questions or problems coding this.

See ya again soon!



Hehe! Oh, well, after posting my question, I have been searching around this forum and I have definitely noticed the abundance of similar questions.

I should have probably done more research before asking. Sorry 'bout that. :frowning:

I do really appreciate the help. :cool:

Ahh, that is a good idea! Makes perfect sense to me. :slight_smile:

Nope, and done.

More excellent tips. Sounds good to me.

Ahhhh, works perfectly! That was way more simple than I expected it to be. Thanks for pushing me in the right direction! :spf:

Just to round things off, here is my setup:

Domain .htaccess:

RewriteRule ^/?sub/(.*)$$1 [R=301,L]

Subdomain .htaccess:

RewriteCond %{HTTP_HOST} ^domain\\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Ahhh, interesting. Good point. In this case, I am not using hotlink protection, but this is great info to know.

DK, you ROCK!

Thanks a billion! I really appreciate the pro help!

Have an excellent day!



No problem. Few people read ANYTHING before posting a question. Oh, well, such is life these days, isn’t it?

I try to make sense with the explanations - remember, I’m here to teach, not to code so I’m thrilled that you were able to get to coding the solution with a gentle nudge in the right direction.

Sorry, I removed the comment about {THE_REQUEST} after noting that you’d not mentioned it - that was in another thread. Good info but irrelevant in your case.

I “preach” “Specificity” as a means to get to verbalizing what I want to do with mod_rewrite code as it clarifies the problem and enables me to see other (potential) problems. All I did for you was convert your problem description to a form that would enable coding with mod_rewrite - YOU did the “heavy lifting” by coding it. :tup:

Okay, a few comments, though:

Thanks for the HUGE pat on the back!



Oh, for sure! I really appreciate all of your help. Every time I have asked a question, you have shown me the light. :slight_smile:

Definitely cool. I mean, I would have never thought to put most of my rewrite code in the subdomain folder. I would still be banging my head on my keyboard if it was not for you! :spf: :slight_smile:

Doh! Sorry, I must have misunderstood. Sorry about that. I guess now I am wondering how else I could get folks to my subdomain? For example, if someone types in:

Sorry if stupid question, but is there a better way to take that request and push it to the sub-domain folder/.htaccess file?

Ah, wait! I just removed the rewrite rule from my main domain .htaccess, and it still gets me to the subdomain! Cool. I did not realize I could get by without that code in my .htaccess file. (:

Thanks so much for corrected code and clarifications! Much appreciated. :slight_smile:

So, the only problem left was that when I typed in this:

I got redirected to this:

My fix:

RewriteCond %{HTTP_HOST} !sub\\.domain\\.com [NC]
RewriteRule .?{REQUEST_URI} [R=301,L]
RewriteRule ^/?sub/(.*)$$1 [R=301,L]

Cool! I hope I got it right this time (I am wondering if I can combine the two rewrite rules?).

Thanks a billion DK! I owe you one. :slight_smile:


Shazbat! That subdirectory {REQUEST_URI} didn’t take the subdirectory off the path/to/filename! Let’s change that code to:

RewriteCond %{HTTP_HOST} !sub\\.domain\\.com [NC]
[COLOR="Red"]# REMOVE this line
# RewriteRule .?{REQUEST_URI} [R=301,L][/COLOR]
# Assuming that you're hosted on Apache 2.x, delete the red below
RewriteRule ^[COLOR="Red"]/?[/COLOR]sub/(.*)$$1 [R=301,L]



Thanks DK! I really appreciate it!!! :slight_smile: :spf:

Have an excellent December/New Years!


Enjoy your holiday season, too, Mickey! :santa: