Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to rewrite a sub directory to go the main domain. For example, I have and I want to rewrite to

This is what I used in my htaccess file:

RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteCond %{REQUEST_URI} !^/newsitecontainer/newsite/
RewriteRule (.*) /newsitecontainer/newsite/$1

This rewrite code works fine, but there are 2 issues that I'm concerned with:

1) Within my newsitecontainer there are two dependent folders for the newer site to work properly, "newsite" directory and "data" directory. The data directory needs to be outside of the "newsite" folder (in root). The current (live website) is I want to rewrite those two subdirectories to act/show as the current domain, (except for the "data" directory, which would only need to be in the correct placement to be called by the "newsite" directory).

This is a simple diagram of what my current directory structure:

I guess I want "newsite" url to equal the current domain (, "newsite url" = I want the "data" directory to be rewritten to shows as it's outsite the "newsite" directory, but still within the root directory (newsitecontainer).

I just got hip to this htaccess stuff, so this may sound confused. I know I am...

2) The second issue is once the "newsite" and "data" directory has been successfully rewritten, I want to rewrite the content pages of subdirectories of the "newsite" to show as they're are directly a part of the new rewrite to the current domain ( I want to do this because there are a lot of plugins for the platform that Im using (Elgg) and the urls can be really long and ugly (doesn't help SEO purposes).

So for example (after successfully rewriting issue#1), I would like to rewrite something like this: "" to this "" This would rewrite any reference to the mod directory and it's subdirectories (and actual page) to show a clean url as just the current domain and the actual visible page of the mods directory.

I know this is done all the time and might be easy to do, but given my situation Im not sure how to properly rewrite the said directories.

I would really appreciate help on this. This is about the only thing left for me to do to present this to my client. Sure I can just give him the write url to view the site, but it's all about presentation and I need to complete this to make it fully live and start promoting the site. So please help.

Thanks a million in advance!

share|improve this question

migrated from Jun 13 '11 at 4:02

This question came from our site for professional and enthusiast programmers.

Redirect and rewrite mean different things in terms of htaccess, can you clarify which one you are trying to accomplish? – datasage Jun 13 '11 at 1:24
@datasage: Yes, sorry I meant REWRITE. I would like rewrite everything I mentioned in the post. I'll update it, sorry – gdinari Jun 13 '11 at 13:13

Here's my solution:

Depending on file type, set an environment variable. For example:

RewriteRule (.*)(\.(pdf|jpg|jpeg|gif|png|ico)){1}$ $1$2 [QSA,E=PATH_LOCAL:../staticfiles]

then later on, test if it's a file by concatenating root folder and your environment variable:

# The file exists => re-write it to the FULL filename:

Now with those indications, you may find your way to reach your goal ;)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.