Forum Moderators: phranque

Message Too Old, No Replies

Using Mod Rewrite to have a subdomain show content in folders.

mod rewrite to point a virtual subdomain to a folder

         

jncr

9:56 pm on Nov 22, 2007 (gmt 0)

10+ Year Member



I am having trouble making mod rewrite point a virtual subdomain to a folder (user types in sub.webpage.com and the address bar stays at sub.webpage.com, and the page displays whatever is in webpage.com/sub). After a week of learning mod rewrite and compiling code, I have come up with this:


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!www.webpage.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).webpage.com/([a-z0-9-]+) [NC]
RewriteRule .* %2/%3 [L]

This code works to some extent, but it redirects to the folder instead of being transparent.

Thank you for all future help.

jdMorgan

10:52 pm on Nov 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will need to provide some way for the code to recognize when it has already done the rewrite. Otherwise you will get a loop. A good way to do both this and to keep your site organized is to put all subdomain content into a subdirectory named "/subs" (just for example) and to test the requested URL-path for that value before rewriting:

RewriteEngine on
RewriteBase /
#
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9\-]+)\.example\.com
RewriteRule (.*) /subs/%2/$1 [L]

There are other more complicated ways to do this as well. But simple is good.

The critical problem with your code was that it was attempting to get the URL-path (%3) from the HTTP_HOST. But the URL-path is not part of HTTP_HOST.

Allow only lowercase subdomains; Apache will treat /subs/my-sub as a different directory than /subs/My-Sub

Also, either do not end-anchor the domain name patterns, or add (:[0-9]+)?$ to the end, in order to allow for valid port numbers passed by some clients in the HTTP "Host:" header.

Jim

[edited by: jdMorgan at 10:56 pm (utc) on Nov. 22, 2007]

jncr

11:17 pm on Nov 22, 2007 (gmt 0)

10+ Year Member



Great thanks Jim! Also, how do I make the 404 error page say "sub.domain.com not found" instead of "/subs/sub/ not found"?

jdMorgan

11:25 pm on Nov 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A normal 404 page does not print the page name or change the address in the browser address bar, so I'm not sure why you have this problem. Take a look at your custom 404 page, and look for scripts included on it that might be printing the "wrong" URL because it's based on the wrong server variables.

Also make sure that the ErrorDocument directive specifies a local filepath, not a canonical URL! If it specifies a URL, the status code returned to the client will be a 302-Found, not a 404-Not Found, as described in the ErrorDocument documentation.

Jim

[edited by: jdMorgan at 11:26 pm (utc) on Nov. 22, 2007]

jncr

11:30 pm on Nov 22, 2007 (gmt 0)

10+ Year Member



Fixed

jncr

5:23 pm on Nov 23, 2007 (gmt 0)

10+ Year Member



Ok I have another problem. If I type in sub.domain.com/folder/folder/ then it redirects to domain.com/subs/sub/folder/folder. How do I fix this?

PS. I was also wondering how to redirect to a file if the virtual subdomain is non-existent.

Thanks for all future help.

jdMorgan

6:31 pm on Nov 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> If I type in sub.domain.com/folder/folder/ then it redirects to domain.com/subs/sub/folder/folder. How do I fix this?

The code above does not redirect, so there is some other code in httpd.conf, conf.d, .htaccess or one of your scripts that is doing this.

Jim