Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have to rewrite a sub directory to go the main domain. For example, I have www.mysite.com and I want to rewrite www.mysite.com/newersite to www.mysite.com

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 www.mysite.com. I want to rewrite those two subdirectories to act/show as the current domain, www.mysite.com (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:

http://i56.tinypic.com/s5y1ja.png

I guess I want "newsite" url to equal the current domain (www.mysite.com), "newsite url" = www.mysite.com. 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 (www.mysite.com). 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: "www.mysite.com/modfolder/submodfolder/visible-page/" to this "www.mysite.com/visible-page/" 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
    
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
add comment

migrated from stackoverflow.com Jun 13 '11 at 4:02

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

1 Answer

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:

RewriteCond %{DOCUMENT_ROOT}/%{ENV:PATH_LOCAL}/%{REQUEST_FILENAME}  -f
# The file exists => re-write it to the FULL filename:
RewriteRule  ^(.+) %{DOCUMENT_ROOT}/%{ENV:PATH_LOCAL}/%{REQUEST_FILENAME} [QSA,L]

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

share|improve this answer
add comment

Your Answer

 
discard

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.