Forum Moderators: phranque
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.
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]
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
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?
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]
Jim