homepage Welcome to WebmasterWorld Guest from 54.242.126.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Wildcard subdomains question
[R] is ok but [L] fails
wulawula

5+ Year Member



 
Msg#: 5143 posted 3:11 am on Dec 17, 2005 (gmt 0)

I know this is an old topic and there have been many threads already. I've resolved anything.example.com to example.com in DNS settings and set up virtual hosts in http.conf:
<VirtualHost 1.1.1.1>
ServerAlias example.com *.example.com
ServerName www.example.com
DocumentRoot /home/example.com/public_html
</VirtualHost>

Now the difficult part is .htaccess. I use the following codes:

##################################################
RewriteEngine On

# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST}.
RewriteCond %{HTTP_HOST}!^www\. [NC]

# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI}^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]

# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3!^(.*)<>\1$[NC]

# Rewrite to /subdomain/path
RewriteRule ^(.*)/%1/$1[R] <---?

##################################################

Everytime I use [L] in the last line, it fails with a 500 error. While if I use [R], it works but the problem is when
a request of http ://anything.example.com will be redirected to http ://example.com/anything/ instead of going there secretly.

Thanks in advance.

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5143 posted 3:10 pm on Dec 17, 2005 (gmt 0)

wulawula,

Welcome to WebmasterWorld!

I'm not sure if this is a typo or copy/paste problem, or what, but the last line must have three spaces in it, like so:

RewriteRule (.*) /%1/$1 [L]

If it does not work with the corrected syntax, then examine your server's raw error log and look at the requested and rewritten paths. If the rewritten path is wrong, you may need to adjust some things based on what you see there.

There are many other spaces missing in the code you posted, but I assume that's because this forum strips spaces in certain positions.

Also be aware that this code will only work on servers supporting POSIX 1003.2 (or later) regular expressions. If installed on a different server, the code may fail (Since it appears to 'work a little' on your server, this is not the problem, but consider this for the future).

Jim

wulawula

5+ Year Member



 
Msg#: 5143 posted 8:55 am on Dec 18, 2005 (gmt 0)

thanks jim. You have been a really good guy for I found your name in lots of threads resolving similar problems.

I have these spaces in the code. This board system strips them.
I'm using apache 1.3.34 on freebsd 4.11 if it matters.

What's strange to me is that [R] works but [L] gives a 500 error. Can this be a clue to this problem?

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5143 posted 3:21 pm on Dec 18, 2005 (gmt 0)

You're going to need to do some testing to identify the problem. Whenever you get a 500-Server Error, look at your raw server error logs to find out what caused the problem.

You may have a mod_alias directive in httpd.conf that will require you to modify the code in .htaccess, or there's some other problem. But the data in the server error log will be much more useful than any guesses here.

You can also try a simple, fixed subdomain->subdirectory rewrite just to see if they work at all. Something like:

# Rewrite a particular subdomain for testing
RewriteCond %{HTTP_HOST} ^([i]one_of_your_subdomains[/i])\.example\.com
# Prevent a rewrite loop
RewriteCond %{REQUEST_URI} !^/[i]one_of_your_subdomains[/i]/
# Rewrite to /subdomain/path
RewriteRule (.*) /%1/$1 [L]

If that doesn't work, then there is probably a server config entry that's interfering with the paths in the code.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved