RegEx in URL Rewrite

From the web-root, I have 3 folders:

files
framework
www

They act as subdomains to my website. For the first 2, I simply want to first ensure that request is not directly requesting into the folders, and secondly, just serve up the file/folder if they exist, ie:

files.domain.com/folder (rewrites to) /files/folder (and if the folder exists, attempts to display its contents)
files.domain.com/doc.txt (rewrites to) /files/doc.txt (and if the file exists, attempts to serve it to user)

but the user should NOT be able to request the subdomain folder directly like this:

domain.com/files/doc.txt (where the subdomain folder is directly requested rather than using files.domain.com)

Secondarily, I’d like that if NO subdomain (or the www) is detected, it do a rewrite into the www folder as such:

www.domain.com (rewrites to) /www/index.cfm?request=/
www.domain.com/this/that (rewrites to) /www/index.cfm?request=/this/that
domain.com/folder (rewrites to) /www/index.cfm?request=/folder

Can anyone possibly help? :frowning:

*** UPDATE ***

And just to show I’m not blindly asking for help (I would LOVE if someone could explain their process to a solution) here is the (faulty) rules I have which so far are just leading me to a brick wall.

[FONT=courier new]RewriteEngine On

#These subdomains we just pass through…
RewriteCond %{HTTP_HOST} ^(files|framework)\. [NC]
RewriteCond %{REQUEST_URI} !^\/(files|framework) [NC]
RewriteRule ^(.*)$ \/%1%{REQUEST_URI} [R=301,L][/FONT]

I thought this would check if the subdomain host had “files.” or “framework.” in the beginning and then ensure that the requested URI does not begin with “/files” or “/framework” and if so, rewrite to (for example), “/files/doc.txt” But sure enough I’m just getting 500 server errors when I test it out.

am,

Drop back ten and consider the DocumentRoot of these subdomains (each is treated as a completely separate domain although they can all be “parked” on the main domain - which your code indicates is what you’re trying to catch).

If you have files and framework created with their DocumentRoot being in those subdirectories of www (that should BE the main domain’s DocumentRoot), neither subdomain request can access the other and neither can access the www (main domain) DocumentRoot. I’d make my setup like this to avoid “sorting problems.”

That said, the subdomains will take care of themselves but it’s your domain/files or domain/framework which needs to be addressed by checking for those directories in the request (in the domain’s DocumentRoot .htaccess file) and, if found, redirect to the subdomain to handle the request:

RewriteEngine on
RewriteRule (files|framework)/(.*)$ http://$1.domain.com/$2 [R=301,L]

Easy enough.

Regards,

DK

Hey DKLynn, thank you for taking the time to respond.

I am going to flesh out my inner thinking just so any holes in them can be filled in. First, I am actually on a Windows environment. We are using a tool in IIS7 that basically emulates 99% of the functionality of the Apache Mod Rewrite. The way I currently organize my projects is, that I make a new folder for each domain and immediately inside it are subfolders for the subdomains of that domain (with ‘www’ being where I put the default website in) So for example, my document root is:

c:\inetpub\wwwroot\aaronmartonecom

Inside it I have the 3 subdomain folders:

c:\inetpub\wwwroot\aaronmartonecom\www
c:\inetpub\wwwroot\aaronmartonecom\files
[B]c:\inetpub\wwwroot\aaronmartonecom\framework

[/B]In IIS I setup a website called “AaronMartoneCom” and it’s root goes only up to the folder with the same name. NORMALLY, I could call the different folder locations by links like “/www/file.txt” or “/files/private/stuff.doc”.

I made “bindings” to the site which indicate the list of host names that the site responds to. I added “www.aaronmartone.com”, “files.aaronmartone.com”, “framework.aaronmartone.com” and “aaronmartone.com”.So if I request any of those addresses, it basically requests the root of the site. I then use the rewriting to read the host, check for which “subdomain” it is requesting, and then rewrite accordingly so that requests like:

files.aaronmartone.com/file.txt (rewrite to) /files/file.txt

LET ME CLARIFY SOMETHING THAT MIGHT BE CAUSING US CONFUSION

In the event that my thought process of subdomain-ing doesn’t match how you do subdomains, let me clarify what’s going on.

I only have 1 site with my host; without having to buy more for each subdomain. My host told me that I can bind unlimited host-name values to it for subdomaining purposes. So I bind “framework.aaronmartone.com”, “files.aaronmartone.com”, “www.aaronmartone.com” and “aaronmartone.com” to it. This way, when a user requests a URL like “files.aaronmartone.com/folder/file.ext” the URL rewrite looks at the host (files.aaronmartone.com) and then goes to my 1 website that has a binding for it and performs a rewrite to “/files/folder/file.ext”.

This is how my host explained that i could use URL rewrite to do subdomaining on my setup. Can I confirm with you that this is similar in process to what you are envisioning, or is this something entirely different?

am,

It’s very different (typical of a WinDoze system, I guess - pardon my aversion to M$ online).

I guess I’m not quite sure how (and definitely now WHY) your host has setup your account as you’ve shown:

  • www
  • files
  • framework

This gets just a bit more complex because of this type of setup (because you need to check for crosslinks between subdomains and subdirectories.

If you want to merely FAIL inappropriate requests:

RewriteCond %{HTTP_HOST} ^(www|files|framework)\\.domain\\.com [NC]
RewriteRule !^%1/ - [F]

If you want to redirect to the appropriate subdomain:

RewriteCond %{IS_SUBREQ} false
RewriteCond %{HTTP_HOST} !^(www|files|framework)\\.domain\\.com [NC]
RewriteRule ^(www|files|framework)/(.*)$ http://$1.domain.com/$1/$2 [R=301,L]

The {IS_SUBREQ} will prevent looping on a redirection whereas the {HTTP_HOST} checks that the request is NOT to a subdomain.

Untested but either should work and it all depends upon what you’re really trying to do. Note, though, that {IS_SUBREQ} may be part of the “1% incompatible” that is claimed for the mod_rewrite-like code you’re using.

Regards,

DK

DKLynn, a sidebar if I may.

These sub-requests. I heard about them through other resources, and someone said they had to do with the looping nature of URL rewriting rules. Can you explain to me what a sub-request is and how it differs from a normal request?

As far as what you’ve listed there, I think I need to clarify something, because I got to speak with a co-worker who used to work in Apache (and I think things are done a little differently in regards to subdomains)

My host confirmed for me that the way they do subdomains on a 1-site account is:

  1. User requests files.domain.com
  2. Host’s DNS files resolve files.domain.com to point to IP address of the server that is hosting my site
  3. IIS gets the request and looks at the host name files.domain.com.
  4. IIS checks to see which of its sites have a binding that matches, my site has one setup, so it sends the request to that site
  5. URL rewriting captures the request, let’s say it’s files.domain.com/folder/file.ext. At this point, we don’t want to do any redirecting BACK to files.domain.com (because we’re already there). Instead, it needs to just rewrite the request as /files/folder/file.ext (hopefully checking if that file exists, and if so then serving up the file)

This might be a very different process than how subdomains are done on Apache/Unix. If I had a plan that allowed me multiple sites in IIS (I don’t) then I could create a site for each subdomain, so that “www.domain.com” requests go to my “www.domain.com” site, “files.domain.com” go to my site setup for “files.domain.com”, etc. But since I don’t, he said the only way I can do subdomaining off 1 site is to redirect requests to subfolders off the root.

I hope that has clarified (I don’t fault you for hating on Windoze; the only 2 things from Microsoft I like are SQL Server and IIS7) :slight_smile:

Now, with all that behind us, the second code set above appears to be what you’re after (to force the subdomain use for the files and frames subdirectories - which should not be accessible from the www subdomain/subdirectory).

Regards,

DK